Skip to content

Instantly share code, notes, and snippets.

@graup
Last active November 1, 2016 05:13
Show Gist options
  • Save graup/2d6eea0d017ba954193e6b9c313f669b to your computer and use it in GitHub Desktop.
Save graup/2d6eea0d017ba954193e6b9c313f669b to your computer and use it in GitHub Desktop.
console.log('Lambda calculus with ES6 syntax');
console.log(
// (\(x,y).x+y)(1,2) => 1+2 => 3
((x, y) => x + y) (1, 2)
);
console.log(
// ((\x.\y.x+y) 1) 2 => (\y.1+y) 2 => 3
(x => y => x + y) (1) (2)
);
console.log(
// (\x.x 1) => fn (N->A) -> B
(x => x(1))
);
console.log(
// (\x.(\y.x y)) => fn (A->B) -> (B->A)
(x => y => x(y))
);
console.log(
// (\x.x 1) (\x.x+1) => (\x.x+1) 1 => 1+1 => 2
(x => x(1)) (x => x+1)
);
console.log(
// (\x.(\y.x y)) (\x.x+1) 2 => (\y.(\x.x+1) y) 2 => (\x.x+1) 2 => 2+1 => 3
(x => y => x(y)) (x => x+1) (2)
);
// Y combinator
// \f.(\x.f (x x)) (\x.f (x x))
// Translated from https://gist.github.com/logicmason/0722b5b159a45f7a81b6
var Y = fn =>
(x => fn(y => x(x)(y)))
(x => fn(y => x(x)(y)))
;
var factgen = (fact) =>
n => (n === 0) ? 1 : n * fact(n-1)
;
var factorial = Y(factgen);
console.log(factorial(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment