Created
April 13, 2012 20:55
-
-
Save nitin-motiani/2380137 to your computer and use it in GitHub Desktop.
Y combinator 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 Y = function(g) { | |
var recur = function (f) { | |
return function(n) { | |
return g(f(f))(n); | |
}; | |
}; | |
return recur(recur); | |
}; | |
var fact = function(rec) { | |
return function(n) { | |
if (n < 2) | |
{ | |
return n; | |
} | |
return n * rec(n - 1); | |
}; | |
}; | |
var factorial = Y(fact); | |
print(factorial(11)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment