fixpoint-combinators

Haskell: to fix or not to fix

蓝咒 提交于 2019-11-30 00:42:52
问题 I recently learned about Data.Function.fix, and now I want to apply it everywhere. For example, whenever I see a recursive function I want to " fix " it. So basically my question is where and when should I use it. To make it more specific: 1) Suppose I have the following code for factorization of n : f n = f' n primes where f' n (p:ps) = ... -- if p^2<=n: returns (p,k):f' (n `div` p^k) ps for k = maximum power of p in n -- if n<=1: returns [] -- otherwise: returns [(n,1)] If I rewrite it in

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

Fixed point combinator in Haskell

十年热恋 提交于 2019-11-27 17:50:55
问题 The fixed point combinator doesn't always produce the right answer given the definition: fix f = f (fix f) The following code does not terminate: fix (\x->x*x) 0 Of course, fix can't always produce the right answer, but I was wondering, can this be improved? Certainly for the above example, one can implement some fix that looks like fix f x | f x == f (f x) = f x | otherwise = fix f (f x) and gives the correct output. What is the reason the above definition (or something even better, as this

Why does GHC make fix so confounding?

匆匆过客 提交于 2019-11-27 09:14:14
Looking at the GHC source code I can see that the definition for fix is: fix :: (a -> a) -> a fix f = let x = f x in x In an example fix is used like this: fix (\f x -> let x' = x+1 in x:f x') This basically yields a sequence of numbers that increase by one to infinity. For this to happen fix must be currying the function that it receives right back to that very function as it's first parameter. It isn't clear to me how the definition of fix listed above could be doing that. This definition is how I came to understand how fix works: fix :: (a -> a) -> a fix f = f (fix f) So now I have two

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

Why does GHC make fix so confounding?

六眼飞鱼酱① 提交于 2019-11-26 14:28:23
问题 Looking at the GHC source code I can see that the definition for fix is: fix :: (a -> a) -> a fix f = let x = f x in x In an example fix is used like this: fix (\f x -> let x' = x+1 in x:f x') This basically yields a sequence of numbers that increase by one to infinity. For this to happen fix must be currying the function that it receives right back to that very function as it's first parameter. It isn't clear to me how the definition of fix listed above could be doing that. This definition

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