我想编写一个函数mapsucc,它生成列表中每个项的后继.很容易:
mapsucc xs = map succ xs
Run Code Online (Sandbox Code Playgroud)
工作得很好.我想我可以使用Currying来使这个定义更短:
mapsucc = map succ
Run Code Online (Sandbox Code Playgroud)
但我得到:
No instance for (Enum b0) arising from a use of ‘succ’
The type variable ‘b0’ is ambiguous
Relevant bindings include
mapsucc :: [b0] -> [b0] (bound at mapsucc.hs:1:1)
Note: there are several potential instances:
instance Enum Ordering -- Defined in ‘GHC.Enum’
instance Enum Integer -- Defined in ‘GHC.Enum’
instance Enum () -- Defined in ‘GHC.Enum’
...plus six others
In the first argument of ‘map’, namely ‘succ’
In the expression: map succ
In an equation for ‘mapsucc’: mapsucc = map succ
Run Code Online (Sandbox Code Playgroud)
现在,我不清楚为什么curried版本应该是模糊的,相反,为什么具有显式参数的那个不是.
另外,如果从ghci我使用let声明mapsucc,它可以完美地工作:
Prelude> let mapsucc = map succ
Prelude> mapsucc "hello"
"ifmmp"
Run Code Online (Sandbox Code Playgroud)
如果我重新定义succ,行为相同:
mysucc x = succ x
Run Code Online (Sandbox Code Playgroud)
完美的,同时
mysucc = succ
Run Code Online (Sandbox Code Playgroud)
给出了同样的错误:
No instance for (Enum a0) arising from a use of ‘succ’
The type variable ‘a0’ is ambiguous
Relevant bindings include
mysucc :: a0 -> a0 (bound at mapsucc.hs:3:1)
Note: there are several potential instances:
instance Enum Ordering -- Defined in ‘GHC.Enum’
instance Enum Integer -- Defined in ‘GHC.Enum’
instance Enum () -- Defined in ‘GHC.Enum’
...plus six others
In the expression: succ
In an equation for ‘mysucc’: mysucc = succ
Run Code Online (Sandbox Code Playgroud)
而且,如果我通过let定义了ghci中的mysucc,它可以完美地运行:
Prelude> let mysucc = succ
Prelude> mysucc 3
4
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
448 次 |
| 最近记录: |