{- CSE 341. Y combinator in Haskell (with unsafeCoerce to make it work) -} import Unsafe.Coerce -- y won't typecheck in Haskell because it would require an infinite type. -- However, we can convince Haskell to accept it anyway using unsafeCoerce y :: (a -> a) -> a y = \f -> (\x -> f (unsafeCoerce x x)) (\x -> f (unsafeCoerce x x)) -- and here is a factorial definition using the genuine y combinator: fact = y (\f -> (\n -> if n==0 then 1 else n*f (n-1)))