Skip to content

Instantly share code, notes, and snippets.

@mofas
Last active May 9, 2018 06:16
Show Gist options
  • Save mofas/85b7243940f649d4ff5ce0898784f1c4 to your computer and use it in GitHub Desktop.
Save mofas/85b7243940f649d4ff5ce0898784f1c4 to your computer and use it in GitHub Desktop.
y-combinator
// 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