accumulate :: (a -> a) -> a -> (a -> a) -> a -> (a -> a) -> a
accumulate combiner null_value term x next n = iter x null_value
where iter x result = if x > n
then result
else iter (next x) (combiner (term x) result)
这在没有类型签名的情况下运行完美,但是使用类型签名,我一直遇到这个错误:
Couldn't match expected type `a' with actual type `a -> a'
`a' is a rigid type variable bound by
the type signature for
accumulate :: (a -> a) -> a -> (a -> a) -> a -> (a -> a) -> a
at haskell-sicp/chapter1.hs:131:15
In the expression: iter x null_value
In an equation for `accumulate':
accumulate combiner null_value term x next n
= iter x null_value
where
iter x result
= if x > n then
result
else
iter (next x) (combiner (term x) result)
我是haskell的新手,但我不明白我的签名有什么问题?
答案 0 :(得分:4)
我可以告诉您,combiner
方法需要两个a
并返回一个a
,因此它应为(a -> a -> a)
,{{1需要标记为a
,并且您忘记表示该函数返回Ord
。所以,你的实际类型签名应该是
a