Last active
July 3, 2019 04:43
-
-
Save djleonskennedy/22e09738035573ce493e419a0f89d598 to your computer and use it in GitHub Desktop.
Y Combinator http://r6.ca/blog/20060919T084800Z.html create by https://stackoverflow.com/users/9859/rampion
This file contains 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
-- 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