在解释器中工作时,将函数绑定到名称通常很方便,例如:
ghci> let f = (+1)
ghci> f 1
2
这是别名f
到函数(+1)
。简单的。
然而,这并不总是有效。我发现导致错误的一个例子是尝试使用别名nub
来自Data.List
模块。例如,
ghci> :m Data.List
ghci> nub [1,2,2,3,3,3]
[1,2,3]
ghci> let f = nub
ghci> f [1,2,2,3,3,3]
<interactive>:1:14:
No instance for (Num ())
arising from the literal `3'
Possible fix: add an instance declaration for (Num ())
In the expression: 3
In the first argument of `f', namely `[1, 2, 2, 3, ....]'
In the expression: f [1, 2, 2, 3, ....]
但是,如果我明确陈述论点x
然后它就可以正常工作:
ghci> let f x = nub x
ghci> f [1,2,2,3,3,3]
[1,2,3]
谁能解释这种行为?