Last active
November 1, 2016 05:13
-
-
Save graup/2d6eea0d017ba954193e6b9c313f669b to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
); |
This file contains 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
// 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