[Functional Programming] Church Encodings: Numberals
作者:互联网
const log = console.log; // zero :: &fa.a const zero = f => x => x; // zero is F // once :: &fa.fa const once = f => x => f(x); // once it I // twice :: &fa.f(fa) const twice = f => x => f(f(x)); // thrice :: &fa.f(f(fa)) const thrice = f => x => f(f(f(x))); const T = true; const F = false; const I = x => x; const not = x => !x; log(zero(not)(T)) // true, because only return second arguement log(once(not)(T)) // false log(twice(not)(F)) // false log(thrice(not)(T)) // false log('****') /** SUCCSOR SUCC N1 = N2 SUCC N2 = N3 SUCC(SUCC N1) = N3 SUCC &fa.fa = &fa.f(fa) SUCC N2, then n is 2, do f n times, then add one f more */ const succ = n => f => x => f(n(f)(x)); // conver chunch number to JS number. // jsnum :: take a chunch number, call (x => x + 1) n times, and start from 0. const jsnum = n => n(x => x + 1)(0); log(succ(zero)(not)(T)) // false log(jsnum(succ(zero))) // 1 log(jsnum(succ(succ(zero)))) // 2 const n0 = zero; const n1 = once; const n2 = twice; const n3 = thrice; const n4 = succ(thrice); log(jsnum(succ(n2))) // 3
标签:const,log,SUCC,Programming,Functional,succ,fa,zero,Encodings 来源: https://www.cnblogs.com/Answer1215/p/10844317.html