Created
March 25, 2014 17:41
-
-
Save Soft/9767133 to your computer and use it in GitHub Desktop.
Church numerals
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
var addOne = function (x) { return x+1; }; | |
var church = function(n) { | |
if (n === 0) { | |
return function(fn) { | |
return function(x) { | |
return x; | |
} | |
} | |
} else { | |
return function(fn) { | |
return function(x) { | |
return fn(church(n-1)(fn)(x)); | |
} | |
} | |
} | |
}; | |
var unchurch = function(fn) { | |
return fn(addOne)(0); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment