Last active
December 15, 2019 20:09
-
-
Save psqq/aee6b98839ae4878d219 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LAMBDA WORLD ================================================================ | |
zero = p => x => x; | |
one = p => x => p(x); | |
inc = n => p => x => p(n(p)(x)); | |
two = inc(one); | |
three = inc(two); | |
add = n => m => m(inc)(n); | |
five = add(two)(three); | |
mul = n => m => m(add(n))(zero); | |
fifeteen = mul(three)(five); | |
ten = add(five)(five); | |
hundred = mul(ten)(ten); | |
pair = x => y => f => f(x)(y); | |
left = p => p(x => y => x); | |
right = p => p(x => y => y); | |
p1 = pair(three)(five); | |
lp1 = left(p1); | |
rp1 = right(p1); | |
nextp = p => pair(right(p))(inc(right(p))); | |
dec = n => left(n(nextp)(pair(zero)(zero))); | |
ninetynine = dec(hundred); | |
sub = n => m => m(dec)(n); | |
eightyfive = sub(hundred)(fifeteen); | |
np1 = nextp(p1); | |
ltrue = x => y => x; | |
lfalse = x => y => y; | |
not = b => b(lfalse)(ltrue); | |
and = a => b => a(b(ltrue)(lfalse))(lfalse); | |
or = a => b => a(ltrue)(b(ltrue)(lfalse)); | |
iszero = n => n(x => lfalse)(ltrue); | |
le = n => m => iszero(sub(n)(m)); | |
ge = n => m => iszero(sub(m)(n)); | |
eq = n => m => and(le(n)(m))(ge(n)(m)); | |
empty = pair(lfalse)(lfalse); | |
shift = a => n => pair(ltrue)(pair(n)(a)); | |
arr0 = shift(shift(empty)(one))(two); | |
//============================================================================== | |
// printing | |
function pdoc(x) { document.body.innerHTML += x + "<br>"; } | |
function pcon(x) { console.log(x); } | |
var p = pcon; | |
function to_i(n) { return n(x => x + 1)(0); } | |
function pi(n) { p(to_i(n)); } | |
function to_b(b) { return b(true)(false); } | |
function pb(n) { p(to_b(n)); } | |
// (ltrue, (one, (lfalse, lfalse))) | |
function to_a(a, res) { | |
res = res || []; | |
if (to_b(left(a))) { | |
res.push(to_i(left(right(a)))); | |
to_a(right(right(a)), res); | |
} | |
return res; | |
} | |
function pa(a) { p(to_a(a)); } | |
pb(ge(one)(two)) | |
pb(eq(one)(two)) | |
pb(eq(one)(one)) | |
pa(arr1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment