Skip to content

Instantly share code, notes, and snippets.

@victusfate
Created May 6, 2011 12:47
Show Gist options
  • Save victusfate/958892 to your computer and use it in GitHub Desktop.
Save victusfate/958892 to your computer and use it in GitHub Desktop.
Y-Combinator in CoffeeScript
var Y, factorial;
Y = function(le) {
return (function(f) {
return f(f);
})(function(f) {
return le(function(x) {
return f(f)(x);
});
});
};
factorial = Y(function(fac) {
return function(n) {
if (n <= 2) {
return n;
} else {
return n * fac(n - 1);
}
};
});
factorial(5);
Y = (le) ->
((f) -> f f) (f) ->
le (x) ->
f(f) x
factorial = Y (fac) ->
(n) ->
if n <= 2 then n else n * fac n-1
factorial 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment