[Haskell-beginners] cps in yaht

Deng Wu wudeng256 at gmail.com
Sat Jul 13 11:47:45 CEST 2013

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]
CPS> cfold (:) [] [1,2,3]

but, when I try to test this, I find there is a problem, the ghci gives:

*Main> cfold (+) 0 []

    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

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]

So my question comes, is it a bug in the book or something I miss here?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130713/e033fbdd/attachment-0001.htm>

More information about the Beginners mailing list