-
-
Save adnils/6cf38aa5213a46ef6853 to your computer and use it in GitHub Desktop.
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
function Y(le) { | |
return (function (f) { | |
return f(f); | |
}(function (f) { | |
return le(function (x) { | |
return f(f)(x); | |
}); | |
})); | |
} | |
var factorial = Y(function (fac) { | |
return function (n) { | |
return n <= 2 ? n : n * fac(n - 1); | |
}; | |
}); | |
factorial(5); // 120 |
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
let Y = | |
(le => (f => f(f)) | |
(f => le((...args) => f(f)(...args)))); | |
let factorial = | |
Y(f => (n => | |
(n < 2 ? | |
1 : | |
n * f(n - 1)))); | |
factorial(5); // 120 |
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
(define (Y f) | |
((lambda (x) (x x)) | |
(lambda (g) | |
(f (lambda args (apply (g g) args)))))) | |
(define fac | |
(Y | |
(lambda (f) | |
(lambda (x) | |
(if (< x 2) | |
1 | |
(* x (f (- x 1)))))))) | |
(fac 5) ; 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment