【发布时间】:2017-11-30 18:33:17
【问题描述】:
我正在阅读“Haskell 编程”一书。一个练习要求我使用高阶函数定义map f。我选择像下面这样定义map (+1):
unfold p h t x | p x = []
| otherwise = h x : unfold p h t (t x)
-- equivalent to `map (+1)`
mapinc = unfold (==[]) ((+1).head) tail
(直接取自练习题)如果谓词p 对参数值为真,则函数unfold p h t 生成空列表,否则通过将函数h 应用于此生成非空列表value 给出头部,函数t 生成另一个参数,该参数以相同的方式递归处理以产生列表的尾部。
我检查了mapinc 的实现,看起来不错:
*Main> mapinc [1,2,3]
[2,3,4]
但是,在我添加类型声明之后:
mapinc :: Num a => [a] -> [a]
mapinc = unfold (==[]) ((+1).head) tail
然后在WinGHCi中重新加载脚本,它给出了以下错误:
• Could not deduce (Eq a) arising from a use of ‘==’
from the context: Num a
bound by the type signature for:
mapinc :: forall a. Num a => [a] -> [a]
at D:\7a.hs:4:1-29
Possible fix:
add (Eq a) to the context of
the type signature for:
mapinc :: forall a. Num a => [a] -> [a]
• In the first argument of ‘unfold’, namely ‘(== [])’
In the expression: unfold (== []) ((+ 1) . head) tail
In an equation for ‘mapinc’:
mapinc = unfold (== []) ((+ 1) . head) tail
|
5 | mapinc = unfold (==[]) ((+1).head) tail | ^^^^
知道为什么会这样吗?
【问题讨论】:
标签: haskell higher-order-functions