为什么这些函数会因类型错误而失败,而另一个则没有?

And*_*ker 2 haskell

在GHCi中,如果我定义两个函数,如下所示:

> let succ = (+ 1)
> let pred = (- 1)
Run Code Online (Sandbox Code Playgroud)

然后将它们称为两者,如下所示:

> succ 5
> pred 5
Run Code Online (Sandbox Code Playgroud)

为什么one(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
Run Code Online (Sandbox Code Playgroud)

是什么导致类型推断在一个函数中失败而另一个函数失败?

lef*_*out 7

-在Haskell中有一个奇怪的特殊语法规则.它是唯一不能用作左中缀部分的中缀,而是-x始终被解析为单个负数.该符号仅在被识别为中缀时

  • 明确用作减法, 2-1
  • 用作右边部分, (1-)
  • 用作二元前缀函数,(-) 1.

  • 实际上,有`subtract = flip( - )`来制作部分.有点kludgy,但...... (2认同)