# 頭尾成對

December 14, 2021

``````let nil = [undefined, undefined];

let head = ([h, _]) => h;
let tail = ([_, t]) => t;
``````

## 定義 pair

``````let pair = l => r => f => f(l)(r);
``````

``````let left = p => p(l => _ => l);
``````

``````let right = p => p(_ => r => r);
``````

``````let nil = pair(undefined)(undefined);
let head = lt => left(lt);
let tail = lt => right(lt);
``````

``````let nil = pair(undefined)(undefined);
let tail = right;
``````

``````(f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(map => lt => f => (lt => head(lt) === undefined)(lt) ? nil : con(f(head(lt)))(map(tail(lt))(f)))((elems => (lt => (f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rev => r => l => (lt => head(lt) === undefined)(l) ? r : rev(con(head(l))(r))(tail(l)))(nil)(lt))(elems()))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rcon => tail => head => head === undefined ? tail : rcon(con(head)(tail)))(nil)(1)(2)(3)))(elem => elem - 1)`
``````

``````(f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(map => lt => f => (lt => (lt => (p => p(l => _ => l))(lt))(lt) === undefined)(lt) ? ((l => r => f => f(l)(r))(undefined)(undefined)) : (head => tail => (l => r => f => f(l)(r))(head)(tail))(f((lt => (p => p(l => _ => l))(lt))(lt)))(map((lt => (p => p(_ => r => r))(lt))(lt))(f)))((elems => (lt => (f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rev => r => l => (lt => (lt => (p => p(l => _ => l))(lt))(lt) === undefined)(l) ? r : rev((head => tail => (l => r => f => f(l)(r))(head)(tail))((lt => (p => p(l => _ => l))(lt))(l))(r))((lt => (p => p(_ => r => r))(lt))(l)))((l => r => f => f(l)(r))(undefined)(undefined))(lt))(elems()))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rcon => tail => head => head === undefined ? tail : rcon((head => tail => (l => r => f => f(l)(r))(head)(tail))(head)(tail)))((l => r => f => f(l)(r))(undefined)(undefined))(1)(2)(3)))(elem => elem - 1)
``````

## List 翻譯機

``````function array(lt) {
let pair = l => r => f => f(l)(r);
let left = p => p(l => _ => l);
let right = p => p(_ => r => r);

let nil = pair(undefined)(undefined);
let tail = right;

let isEmpty = lt => head(lt) === undefined;

function arr(acc, l) {
if(isEmpty(l)) {
return acc;
} else {
``````console.log(array((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(map => lt => f => (lt => (lt => (p => p(l => _ => l))(lt))(lt) === undefined)(lt) ? ((l => r => f => f(l)(r))(undefined)(undefined)) : (head => tail => (l => r => f => f(l)(r))(head)(tail))(f((lt => (p => p(l => _ => l))(lt))(lt)))(map((lt => (p => p(_ => r => r))(lt))(lt))(f)))((elems => (lt => (f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rev => r => l => (lt => (lt => (p => p(l => _ => l))(lt))(lt) === undefined)(l) ? r : rev((head => tail => (l => r => f => f(l)(r))(head)(tail))((lt => (p => p(l => _ => l))(lt))(l))(r))((lt => (p => p(_ => r => r))(lt))(l)))((l => r => f => f(l)(r))(undefined)(undefined))(lt))(elems()))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rcon => tail => head => head === undefined ? tail : rcon((head => tail => (l => r => f => f(l)(r))(head)(tail))(head)(tail)))((l => r => f => f(l)(r))(undefined)(undefined))(1)(2)(3)))(elem => elem - 1)))