Created
September 17, 2009 12:05
-
-
Save commana/188461 to your computer and use it in GitHub Desktop.
Church encoding in JavaScript
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
var inc = function (n) { | |
return n + 1; | |
}; | |
var zero = function (f) { | |
return function (x) { return x; }; | |
}; | |
var church = function (n) { | |
var loop = function (n, acc) { | |
if (n === 0) return acc; | |
return loop(n-1, function (f) { | |
return function (x) { | |
return f(acc(f)(x)); | |
}; | |
}); | |
}; | |
return loop(n, zero); | |
}; | |
var unchurch = function (f) { | |
return f(inc)(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment