<div dir="ltr"><div class="gmail_default" style="font-size:small">hi, everyone,<br><br></div><div class="gmail_default" style="font-size:small">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:<br>
<br>cfold’ f z [] = z<br>cfold’ f z (x:xs) = f x z (\y -> cfold’ f y xs)<br><br>and gives the test result:<br><br>CPS> cfold (+) 0 [1,2,3,4]<br>10<br>CPS> cfold (:) [] [1,2,3]<br>[1,2,3]<br><br></div><div class="gmail_default" style="font-size:small">
but, when I try to test this, I find there is a problem, the ghci gives:<br><br>-----------------------------------------------------------------------<br>*Main> cfold (+) 0 []<br><br><interactive>:8:7:<br> Occurs check: cannot construct the infinite type:<br>
t10 = (t10 -> t10) -> t10<br> Expected type: t10 -> t10 -> (t10 -> t10) -> t10<br> Actual type: t10 -> t10 -> t10<br> In the first argument of `cfold', namely `(+)'<br> In the expression: cfold (+) 0 []<br>
In an equation for `it': it = cfold (+) 0 []<br>------------------------------------------------------------------------<br><br></div><div class="gmail_default" style="font-size:small">It makes sense to me, so I change the definition of cps to something like this:<br>
<br>cfold f z [] = z<br>cfold f z (x:xs) = (\y -> cfold f y xs) (f x z)<br><br></div><div class="gmail_default" style="font-size:small">And it works fine:<br><br>*Main> cfold (+) 0 [1,2,3]<br>6<br><br></div><div class="gmail_default" style="font-size:small">
So my question comes, is it a bug in the book or something I miss here?<br><br><br><br></div><div class="gmail_default" style="font-size:small">Regards!<br>-<br></div><div class="gmail_default" style="font-size:small">wudeng<br>
</div><div class="gmail_default" style="font-size:small"><br></div></div>