Skip to content

Instantly share code, notes, and snippets.

@nitin-motiani
Created April 13, 2012 20:55
Show Gist options
  • Save nitin-motiani/2380137 to your computer and use it in GitHub Desktop.
Save nitin-motiani/2380137 to your computer and use it in GitHub Desktop.
Y combinator in JavaScript
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