Skip to content

Instantly share code, notes, and snippets.

@djleonskennedy
Last active July 3, 2019 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djleonskennedy/22e09738035573ce493e419a0f89d598 to your computer and use it in GitHub Desktop.
Save djleonskennedy/22e09738035573ce493e419a0f89d598 to your computer and use it in GitHub Desktop.
-- The Y combinator, discovered by Haskell B. Curry, is defined as: Y = \f.(\x.f (x x)) (\x. f (x x))
-- haskell
newtype Mu a = Mu (Mu a -> a)
y f = (\h -> h $ Mu h) (\x -> f . (\(Mu g) -> g) x $ x)
-- javascript
const factSource = partial => n => n === 0 ? 1 : n * partial(n - 1);
// Y Combinator: Y = \f. (\x. f (x x)) (\x. f (x x))
y = f => ( x => n => f(x(x))(n) )(
x => n => f(x(x))(n) );
const fac = y(factSource);
// OR
const Y = f => (x => x(x))(x => f(y => x(x)(y)));
const factorial = f => (x => (x === 1 ? 1 : x * f(x - 1)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment