Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Last active March 9, 2017 14:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinSDK/9c6c82775f422cc87a73 to your computer and use it in GitHub Desktop.
Save JustinSDK/9c6c82775f422cc87a73 to your computer and use it in GitHub Desktop.
Y Combinator in Haskell
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
@weihsiu
Copy link

weihsiu commented Dec 5, 2014

in scala:
def Y[A, B](f: %28A => B%29 => %28A => B%29): A => B = f(Y(f))(_)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment