我正在通过阅读“Yet Another Haskell Tutorial”一书来学习haskell,但在涉及连续传递风格时遇到了一个问题。这本书给出了一个 cps 折叠,如:
cfold’ f z [] = z
cfold’ f z (x:xs) = f x z (\y -> cfold’ f y xs)
并给出测试结果:
CPS> cfold (+) 0 [1,2,3,4]
10
CPS> cfold (:) [] [1,2,3]
[1,2,3]
但是,当我尝试对此进行测试时,我发现有问题,ghci 给出:
*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 []
这对我来说很有意义,所以我将 cps 的定义更改为如下内容:
cfold f z [] = z
cfold f z (x:xs) = (\y -> cfold f y xs) (f x z)
它工作正常:
*Main> cfold (+) 0 [1,2,3]
6
所以我的问题来了,这是书中的错误还是我在这里想念的东西?