Y Combinator in Haskell

前端 未结 5 1460
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 12:44

Is it possible to write the Y Combinator in Haskell?

It seems like it would have an infinitely recursive type.

 Y :: f -> b -> c
 where f :: (f -         


        
5条回答
  •  温柔的废话
    2020-12-07 13:02

    Just to make rampion's code more readable:

    -- Mu :: (Mu a -> a) -> Mu a
    newtype Mu a = Mu (Mu a -> a) 
    
    w :: (Mu a -> a) -> a
    w h = h (Mu h)
    
    y :: (a -> a) -> a
    y f = w (\(Mu x) -> f (w x))
    -- y f = f . y f
    

    in which w stands for the omega combinator w = \x -> x x, and y stands for the y combinator y = \f -> w . (f w).

提交回复
热议问题