Last active
May 9, 2018 06:16
-
-
Save mofas/85b7243940f649d4ff5ce0898784f1c4 to your computer and use it in GitHub Desktop.
y-combinator
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
// basic idea | |
(f => f(f)(5))(f => n => n == 0 ? 1 : (n * f(f)(n-1))); | |
// This version doesn't work, because js is not lazy. | |
// const Y = f => (x => f(x(x)))(x => f(x(x))); | |
// const Y = f => (x => x(x))(x => f(a => x(x)(a))); | |
const Y = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y))); | |
Y(fact => n => n == 0 ? 1 : n*(fact(n-1)))(5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment