
hi, everyone, I'm learning haskell by reading the book <<Yet Another Haskell Tutorial>>, and I encounter a problem when comes to the contiuation passing style. The book gives a cps fold like: cfold’ f z [] = z cfold’ f z (x:xs) = f x z (\y -> cfold’ f y xs) and gives the test result: CPS> cfold (+) 0 [1,2,3,4] 10 CPS> cfold (:) [] [1,2,3] [1,2,3] but, when I try to test this, I find there is a problem, the ghci gives: ----------------------------------------------------------------------- *Main> cfold (+) 0 [] <interactive>:8:7: Occurs check: cannot construct the infinite type: t10 = (t10 -> t10) -> t10 Expected type: t10 -> t10 -> (t10 -> t10) -> t10 Actual type: t10 -> t10 -> t10 In the first argument of `cfold', namely `(+)' In the expression: cfold (+) 0 [] In an equation for `it': it = cfold (+) 0 [] ------------------------------------------------------------------------ It makes sense to me, so I change the definition of cps to something like this: cfold f z [] = z cfold f z (x:xs) = (\y -> cfold f y xs) (f x z) And it works fine: *Main> cfold (+) 0 [1,2,3] 6 So my question comes, is it a bug in the book or something I miss here? Regards! - wudeng

cfold’ f z [] = z cfold’ f z (x:xs) = f x z (\y -> cfold’ f y xs) cfold (+) 0 [] So my question comes, is it a bug in the book or something I miss here?
This can't be correct. In expression `f x z (\y ...)`, function 'f' takes three argument but (+) can only take two. May be you are suppose to pass some other function to cfold'. -- Dilawar EE, IITB

On Sat, Jul 13, 2013 at 4:47 PM, Deng Wu
cfold’ f z (x:xs) = f x z (\y -> cfold’ f y xs)
and gives the test result:
CPS> cfold (+) 0 [1,2,3,4]
The apostrophe matters! Functions cfold and cfold' (read: see-fold prime) are two different functions, for which you gave the definition of one but not the other. To clear up your confusion, try giving the type signatures for both. Daume's text is normally pretty good about this but the section on cfold falls down in this regard. Let me do the cfold one for you: cfold :: (a -> r -> r) -> r -> [a] -> r -- Kim-Ee

You're right! I neglected that there is a apostrophe, I should have been
more careful. Thanks for your help!
Regards!
-
wudeng
On Sat, Jul 13, 2013 at 9:46 PM, Kim-Ee Yeoh
On Sat, Jul 13, 2013 at 4:47 PM, Deng Wu
wrote: cfold’ f z (x:xs) = f x z (\y -> cfold’ f y xs)
and gives the test result:
CPS> cfold (+) 0 [1,2,3,4]
The apostrophe matters!
Functions cfold and cfold' (read: see-fold prime) are two different functions, for which you gave the definition of one but not the other.
To clear up your confusion, try giving the type signatures for both. Daume's text is normally pretty good about this but the section on cfold falls down in this regard.
Let me do the cfold one for you:
cfold :: (a -> r -> r) -> r -> [a] -> r
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sat, Jul 13, 2013 at 05:47:45PM +0800, Deng Wu wrote:
It makes sense to me, so I change the definition of cps to something like this:
cfold f z [] = z cfold f z (x:xs) = (\y -> cfold f y xs) (f x z)
Note that if we just reduce the application of (\y -> ...) to (f x z), we get cfold f z (x:xs) = cfold f (f x z) xs But that is just the usual definition of foldl. So it certainly works fine but it is not in CPS. -Brent

Hi, Brent, thanks for pointing it out, it helps.
Regards!
-
wudeng
On Sun, Jul 14, 2013 at 6:58 AM, Brent Yorgey
On Sat, Jul 13, 2013 at 05:47:45PM +0800, Deng Wu wrote:
It makes sense to me, so I change the definition of cps to something like this:
cfold f z [] = z cfold f z (x:xs) = (\y -> cfold f y xs) (f x z)
Note that if we just reduce the application of (\y -> ...) to (f x z), we get
cfold f z (x:xs) = cfold f (f x z) xs
But that is just the usual definition of foldl. So it certainly works fine but it is not in CPS.
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Brent Yorgey
-
Deng Wu
-
Dilawar Singh
-
Kim-Ee Yeoh
-
吴登