letrec

Sharing vs. non-sharing fixed-point combinator

荒凉一梦 提交于 2019-11-29 14:29:24
问题 This is the usual definition of the fixed-point combinator in Haskell: fix :: (a -> a) -> a fix f = let x = f x in x On https://wiki.haskell.org/Prime_numbers, they define a different fixed-point combinator: _Y :: (t -> t) -> t _Y g = g (_Y g) -- multistage, non-sharing, g (g (g (g ...))) -- g (let x = g x in x) -- two g stages, sharing _Y is a non-sharing fixpoint combinator, here arranging for a recursive "telescoping" multistage primes production (a tower of producers). What exactly does

How do I use fix, and how does it work?

て烟熏妆下的殇ゞ 提交于 2019-11-26 23:48:23
I was a bit confused by the documentation for fix (although I think I understand what it's supposed to do now), so I looked at the source code. That left me more confused: fix :: (a -> a) -> a fix f = let x = f x in x How exactly does this return a fixed point? I decided to try it out at the command line: Prelude Data.Function> fix id ... And it hangs there. Now to be fair, this is on my old macbook which is kind of slow. However, this function can't be too computationally expensive since anything passed in to id gives that same thing back (not to mention that it's eating up no CPU time). What

How do I use fix, and how does it work?

六月ゝ 毕业季﹏ 提交于 2019-11-26 08:47:24
问题 I was a bit confused by the documentation for fix (although I think I understand what it\'s supposed to do now), so I looked at the source code. That left me more confused: fix :: (a -> a) -> a fix f = let x = f x in x How exactly does this return a fixed point? I decided to try it out at the command line: Prelude Data.Function> fix id ... And it hangs there. Now to be fair, this is on my old macbook which is kind of slow. However, this function can\'t be too computationally expensive since