# 未定義是值嗎？

December 14, 2021

``````let yes = f => y => f();
let no = x => f => f();
let when = c => c;
``````

## lambda 表示式就是值

``````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 con = h => t => pair(h)(t);
let tail = right;
``````

## 定義 undef

`undefined` 是值嗎？或者只是個唯一的符號？`undefined` 表示沒有東西、也不是容器、沒有對應的操作，什麼也沒有？

``````let is_undefined = n => n === undefined ? yes : no;
``````

``````let undef = (_ => _)();
``````

``````let is_undef = n => n === undef ? yes : no;
``````

## 重構

``````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 len = l => when(isEmpty(l))
(_ => 0)
(_ => 1 + len(tail(l)));

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 lt = list(elems(1)(2)(3)(4));
console.log(len(lt));    // 4
console.log(sum(lt));    // 10
``````

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

``````let lt2 = map(list(elems(1)(2)(3)))(elem => elem - 1);
console.log(array(lt2));
``````

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

``````(f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(map => l => f => (l => is_undef((p => p(l => _ => l))(l)))(l)(_ => ((l => r => f => f(l)(r))((_ => _)())((_ => _)())))(_ => (h => t => (l => r => f => f(l)(r))(h)(t))(f((p => p(l => _ => l))(l)))(map((p => p(_ => r => r))(l))(f))))((es => (l => ((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rev => r => l => (l => is_undef((p => p(l => _ => l))(l)))(l)(_ => r)(_ => rev((h => t => (l => r => f => f(l)(r))(h)(t))((p => p(l => _ => l))(l))(r))((p => p(_ => r => r))(l)))))((l => r => f => f(l)(r))((_ => _)())((_ => _)()))(l))(es()))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rcon => t => h => is_undef(h)(_ => t)(_ => rcon((h => t => (l => r => f => f(l)(r))(h)(t))(h)(t))))((l => r => f => f(l)(r))((_ => _)())((_ => _)()))(1)(2)(3)))(elem => elem - 1)
``````

``````n => n === (_ => _)() ? (f => y => f()) : (x => f => f())`，成為 `(f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(map => l => f => (l => (n => n === (_ => _)() ? (f => y => f()) : (x => f => f()))((p => p(l => _ => l))(l)))(l)(_ => ((l => r => f => f(l)(r))((_ => _)())((_ => _)())))(_ => (h => t => (l => r => f => f(l)(r))(h)(t))(f((p => p(l => _ => l))(l)))(map((p => p(_ => r => r))(l))(f))))((es => (l => ((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rev => r => l => (l => (n => n === (_ => _)() ? (f => y => f()) : (x => f => f()))((p => p(l => _ => l))(l)))(l)(_ => r)(_ => rev((h => t => (l => r => f => f(l)(r))(h)(t))((p => p(l => _ => l))(l))(r))((p => p(_ => r => r))(l)))))((l => r => f => f(l)(r))((_ => _)())((_ => _)()))(l))(es()))((f => (x => f(n => x(x)(n)))(x => f(n => x(x)(n))))(rcon => t => h => (n => n === (_ => _)() ? (f => y => f()) : (x => f => f()))(h)(_ => t)(_ => rcon((h => t => (l => r => f => f(l)(r))(h)(t))(h)(t))))((l => r => f => f(l)(r))((_ => _)())((_ => _)()))(1)(2)(3)))(elem => elem - 1)
``````