-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionWithUnderscores
41 lines (27 loc) · 2.33 KB
/
functionWithUnderscores
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const Z = f => (x => f(v => x(x)(v)))(x => f(v => x(x)(v)))
const Zero = _ => __ => __ //2
const One = ___ => ____ => ___(____) //4
const True = _____ => ______ => _____ //6
const False = _______ => ________ => ________ //8
const IsZero = _________ => _________(__________ => False)(True) //10
const Pair = ___________ => ____________ => _____________ => _____________(___________)(____________) // 13
const First = ______________ => ______________(True) //14
const Second = _______________ => _______________(False) //15
const Succ = ________________ => _________________ => __________________ => _________________(________________(_________________)(__________________)) //18
const Sum = ___________________ => ____________________ => _____________________ => ______________________ => ____________________(_____________________)(___________________(_____________________)(______________________)) //22
const Pred = _______________________ => First(
_______________________(________________________ => Pair(
Second(________________________))(
Succ(Second(________________________))))
(Pair(Zero)(Zero))) //24
const Mul = _________________________ => __________________________ => __________________________(Sum(_________________________))(Zero) //26
const If = ___________________________ => ____________________________ => _____________________________ => ___________________________(____________________________)(_____________________________) //29
const lif = ______________________________ => _______________________________ => ________________________________ => ______________________________(_______________________________)(________________________________)() //32
const _fact = _________________________________ => __________________________________ => lif(IsZero(__________________________________))(___________________________________ => One)(____________________________________ => Mul(__________________________________)(_________________________________(Pred(__________________________________)))) //36
const fact = Z(_fact)
const decodeNumber = n => n(i => i + 1)(0)
const encodeNumber = n => Array.from(Array(n).keys(), item => item + 1).reduce((p, n) => Succ(p), Zero);
const exec = f => n => decodeNumber(f(encodeNumber(n)));
//console.log(decodeNumber(fact(Five))); //120
//console.log(exec(fact)(5)); //120
exec(fact)(5)