# 真的未定義？

December 15, 2021

``````let len = l => when(isEmpty(l))
(_ => \$0)

let sum = l => when(isEmpty(l))
(_ => \$0)
``````

``````let y = f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n)));

let undef = (_ => _)();
let is_undef = n => n === undef ? yes : no;

let yes = f => _ => f();
let no = _ => f => f();
let when = _ => _;

let not = b => b(_ => no)(_ => yes);

let pair = l => r => f => f(l)(r);
let left = p => p(l => _ => l);
let right = p => p(_ => r => r);

let nil = pair(undef)(undef);
let con = h => t => pair(h)(t);
let tail = right;

let isEmpty = l => is_undef(head(l));

let \$0 = _ => x => x;
let \$1 = f => x => f(x);
let \$2 = f => x => f(f(x));
let \$3 = f => x => f(f(f(x)));

let is_\$0 = n => n(_ => no)(yes);

let succ = n => f => x => f(n(f)(x));
let add = m => n => n(succ)(m);

let pair_succ = p => pair(right(p))(succ(right(p)));
let prev = n => left(n(pair_succ)(pair(\$0)(\$0)));
let sub = m => n => n(prev)(m);

let len = l => when(isEmpty(l))
(_ => \$0)

let sum = l => when(isEmpty(l))
(_ => \$0)

let rcon = t => h => when(is_undef(h))
(_ => t)
(_ => rcon(con(h)(t)));

let rev = r => l => when(isEmpty(l))
(_ => r)

let reverse = l => rev(nil)(l);

let elems = rcon(nil);

let list = es => reverse(es());

let map = l => f => when(isEmpty(l))
(_ => nil)

let lt = list(elems(\$1)(\$2)(\$3));
let lt2 = map(list(elems(\$1)(\$2)(\$3)))(elem => sub(elem)(\$1));

console.log(natural(len(lt)));
console.log(natural(sum(lt)));
console.log(array(lt2));

function natural(n) {
return n(i => i + 1)(0);
}

function array(lt) {
function arr(acc, l) {
if(isEmpty(l) === yes) {
return acc;
} else {
}
}
return arr([], lt);
}
``````

## 定義 nil

YA~~~~？還是不太甘心啊！都已經將 `===``?:` 逼到 `is_undef` 了，就不能給它們一個痛快嗎？（咦？）…

``````let nil = _ => yes;
``````

`f` 是什麼都不重要，因此用 `_` 來表示就可以了。現在解決了 `isEmpty` 一半的問題，只要遇到 `nil` 就可以傳回 `yes`，那麼其他的 List 呢？只有 `nil` 會直接忽略傳入的函式直接傳回 `yes`，既然如此，其他的 List 會呼叫傳入的函式，那麼傳入的函式一律傳回 `no` 就可以了：

``````let isEmpty = l => l(_ => _ => no);
``````

``````let is_nil = l => l(_ => _ => no);
let isEmpty = is_nil;
``````

## 無數字

``````let is_\$\$ = n => n(_ => no)(no);
``````

``````let \$\$ = _ => _ => yes;
``````

``````let rcon = t => h => when(is_\$\$(h))
(_ => t)
(_ => rcon(con(h)(t)));
``````

YA！終於連 `rcon` 都沒使用 `is_undef` 了，別忘了 `list` 也要做個修改，最後必須傳入 `\$\$`

``````let list = es => reverse(es(\$\$));
``````

## 無用

``````let unit = _ => _;
let no_use = unit;

let yes = f => _ => f(no_use);
let no = _ => f => f(no_use);
let when = unit;
``````

## lambda expression

``````let y = f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n)));

let unit = _ => _;
let no_use = unit;

let yes = f => _ => f(no_use);
let no = _ => f => f(no_use);
let when = unit;

let not = b => b(_ => no)(_ => yes);

let pair = l => r => f => f(l)(r);
let left = p => p(l => _ => l);
let right = p => p(_ => r => r);

let nil = _ => yes;
let con = h => t => pair(h)(t);
let tail = right;

let is_nil = l => l(_ => _ => no);
let isEmpty = is_nil;

let \$0 = _ => x => x;
let \$1 = f => x => f(x);
let \$2 = f => x => f(f(x));
let \$3 = f => x => f(f(f(x)));
let is_\$0 = n => n(_ => no)(yes);

let \$\$ = _ => _ => yes;
let is_\$\$ = n => n(_ => no)(no);

let succ = n => f => x => f(n(f)(x));
let add = m => n => n(succ)(m);

let pair_succ = p => pair(right(p))(succ(right(p)));
let prev = n => left(n(pair_succ)(pair(\$0)(\$0)));
let sub = m => n => n(prev)(m);

let len = l => when(isEmpty(l))
(_ => \$0)

let sum = l => when(isEmpty(l))
(_ => \$0)

let rcon = t => h => when(is_\$\$(h))
(_ => t)
(_ => rcon(con(h)(t)));

let rev = r => l => when(isEmpty(l))
(_ => r)

let reverse = l => rev(nil)(l);

let elems = rcon(nil);

let list = es => reverse(es(\$\$));

let map = l => f => when(isEmpty(l))
(_ => nil)

let lt = list(elems(\$1)(\$2)(\$3));
let lt2 = map(list(elems(\$1)(\$2)(\$3)))(elem => sub(elem)(\$1));

console.log(natural(len(lt)));
console.log(natural(sum(lt)));
console.log(array(lt2));

function natural(n) {
return n(i => i + 1)(0);
}

function array(lt) {
function arr(acc, l) {
return when(isEmpty(l))
(() => acc)
}
return arr([], lt);
}
``````

``````(f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(m => l => f => (_ => _)((l => l(_ => _ => (_ => f => f(_ => _))))(l))(_ => (_ => (f => _ => f(_ => _))))(_ => (h => t => (l => r => f => f(l)(r))(h)(t))(f((p => p(l => _ => l))(l)))(m((p => p(_ => r => r))(l))(f))))((e => (l => (f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(v => r => l => (_ => _)((l => l(_ => _ => (_ => f => f(_ => _))))(l))(_ => r)(_ => v((h => t => (l => r => f => f(l)(r))(h)(t))((p => p(l => _ => l))(l))(r))((p => p(_ => r => r))(l))))(_ => (f => _ => f(_ => _)))(l))(e(_ => _ => (f => _ => f(_ => _)))))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(r => t => h => (_ => _)((n => n(_ => (_ => f => f(_ => _)))(_ => f => f(_ => _)))(h))(_ => t)(_ => r((h => t => (l => r => f => f(l)(r))(h)(t))(h)(t))))(_ => (f => _ => f(_ => _)))(f => x => f(x))(f => x => f(f(x)))(f => x => f(f(f(x))))))(e => (m => n => n(n => (p => p(l => _ => l))(n(p => (l => r => f => f(l)(r))((p => p(_ => r => r))(p))((n => f => x => f(n(f)(x)))((p => p(_ => r => r))(p))))((l => r => f => f(l)(r))(_ => _)(_ => x => x))))(m))(e)(f => x => f(x)))
``````

``````function array(lt) {
let unit = _ => _;
let no_use = unit;

let no = _ => f => f(no_use);
let when = unit;

let left = p => p(l => _ => l);
let right = p => p(_ => r => r);

let tail = right;

let is_nil = l => l(_ => _ => no);
let isEmpty = is_nil;

function natural(n) {
return n(i => i + 1)(0);
}

function arr(acc, l) {
return when(isEmpty(l))
(() => acc)
}
return arr([], lt);
}
``````

``````console.log(
array(

(f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(m => l => f => (_ => _)((l => l(_ => _ => (_ => f => f(_ => _))))(l))(_ => (_ => (f => _ => f(_ => _))))(_ => (h => t => (l => r => f => f(l)(r))(h)(t))(f((p => p(l => _ => l))(l)))(m((p => p(_ => r => r))(l))(f))))((e => (l => (f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(v => r => l => (_ => _)((l => l(_ => _ => (_ => f => f(_ => _))))(l))(_ => r)(_ => v((h => t => (l => r => f => f(l)(r))(h)(t))((p => p(l => _ => l))(l))(r))((p => p(_ => r => r))(l))))(_ => (f => _ => f(_ => _)))(l))(e(_ => _ => (f => _ => f(_ => _)))))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(r => t => h => (_ => _)((n => n(_ => (_ => f => f(_ => _)))(_ => f => f(_ => _)))(h))(_ => t)(_ => r((h => t => (l => r => f => f(l)(r))(h)(t))(h)(t))))(_ => (f => _ => f(_ => _)))(f => x => f(x))(f => x => f(f(x)))(f => x => f(f(f(x))))))(e => (m => n => n(n => (p => p(l => _ => l))(n(p => (l => r => f => f(l)(r))((p => p(_ => r => r))(p))((n => f => x => f(n(f)(x)))((p => p(_ => r => r))(p))))((l => r => f => f(l)(r))(_ => _)(_ => x => x))))(m))(e)(f => x => f(x)))

)
);
``````