Last active
March 9, 2017 14:33
-
-
Save JustinSDK/9c6c82775f422cc87a73 to your computer and use it in GitHub Desktop.
Y Combinator in Haskell
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
import Unsafe.Coerce | |
y :: (a -> a) -> a | |
y = \f -> (\x -> f (unsafeCoerce x x))(\x -> f (unsafeCoerce x x)) | |
-- Ref: [Y Combinator in Haskell](http://stackoverflow.com/questions/4273413/y-combinator-in-haskell) | |
-- a fibonacci example | |
main = putStrLn $ show $ y (\fact -> \n -> if n < 2 then 1 else n * fact (n - 1)) 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in scala:
def Y[A, B](f: %28A => B%29 => %28A => B%29): A => B = f(Y(f))(_)