在GHCi中,如果我定义两个函数:
> let succ = (+ 1)
> let pred = (- 1)
然后将它们称为两者,如下:
> succ 5
> pred 5
为什么一个(succ
)运行正常,而另一个运行失败并出现以下错误?
<interactive>:3:1:
Could not deduce (Num (a0 -> t))
arising from the ambiguity check for ‘it’
from the context (Num (a -> t), Num a)
bound by the inferred type for ‘it’: (Num (a -> t), Num a) => t
at <interactive>:3:1-6
The type variable ‘a0’ is ambiguous
When checking that ‘it’
has the inferred type ‘forall a t. (Num (a -> t), Num a) => t’
Probable cause: the inferred type is ambiguous
是什么导致类型推断在一个函数中失败而在另一个函数中失败?
答案 0 :(得分:7)
-
在Haskell中有一个奇怪的特殊语法规则。它是唯一不能用作左中缀部分的中缀,而-x
将始终被解析为单个负数。
2-1
(1-)
(-) 1
。