为什么`-`(减号)不适用于操作员部分? [英] Why doesn't `-` (minus) work for operator sections?

查看:142
本文介绍了为什么`-`(减号)不适用于操作员部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个位置,Haskell中的部分应用程序得到了正确的答案。

  Prelude> (/ 2)10 
5.0
Prelude> (2 /)10
0.2
Prelude> (+3)10
13
Prelude> (3+)10
13

然而,对于 - 运算符,我得到一个错误( - 3)作为Haskell(似乎)将其解释为值 -3 不是部分应用程序。

  Prelude> (-3)10 

<互动>:4:1:
无法推断(Num(a0-> t))
因歧义检查' ('N(a - > t),Num a)
被'it'的推断类型所约束:$(Num(a - > t),Num a)= > t
at< interactive>:4:1-7
类型变量'a0'不明确
当检查'it'时
具有推断类型'forall a t 。 (Num(a→t),Num(a)=> t'
可能的原因:推断的类型不明确

如何解决此问题以获取 7 在这个例子中?

解决方案

使用 减去 - 是Haskell中唯一一个在前缀二进制中缀变体中出现的运算符:

  let a = -3  - 前缀变体
let b =(-3) - 也是前缀变体!
let c = 4 - 3 - binary variant

因此,您必须使用(减3)10 。另请参阅 Haskell 2010报告中的第3.4节(我的重点):


$ b


特殊形式 -e 表示前缀否定,Haskell ,并且是 negate(e)的语法。二元 - 操作符不一定指Prelude中 - 的定义;它可能会被模块系统反弹。但是,一元 - 将始终引用Prelude中定义的 negate 函数。 - 运算符的本地含义与一元否定之间没有关系。



前缀否定具有相同的优先顺序作为Prelude中定义的中缀运算符 - (见表4.1)。因为 e1-e2 解析为二元运算符 - 的中缀应用程序,所以必须写 e1(-e2)替代解析。类似地,( - )(\ xy - > xy)的语法,与任何中缀运算符一样,并且不表示(\ x - > -x) - 对此必须使用 negate

第3.5节总结(再强调一下):


因为 - 在语法中被特别处理,( - exp)不是一个部分,而是一个前缀否定的应用,如前一节所述。但是,

 中定义了一个减法函数,使得(减去exp)相当于不允许的部分。表达式(+( -  exp))可以达到同样的目的。 



Based on the position, the partial applications in Haskell gets the correct answer.

Prelude> (/2) 10
5.0
Prelude> (2/) 10
0.2
Prelude> (+3) 10
13
Prelude> (3+) 10
13

However, for - operator, I got an error with (-3) as Haskell (seems to) interprets it as a value -3 not partial application.

Prelude> (-3) 10

<interactive>:4: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>:4:1-7
    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

How to solve this issue to get 7 in this example?

解决方案

Use subtract. - is the only operator in Haskell, that occurs both in a prefix and binary infix variant:

let a = -3     -- prefix  variant
let b = (-3)   -- also prefix variant!
let c = 4 - 3  -- binary variant

Therefore, you would have to use (subtract 3) 10. See also section 3.4 in the Haskell 2010 report (emphasis mine):

The special form -e denotes prefix negation, the only prefix operator in Haskell, and is syntax for negate (e). The binary - operator does not necessarily refer to the definition of - in the Prelude; it may be rebound by the module system. However, unary - will always refer to the negate function defined in the Prelude. There is no link between the local meaning of the - operator and unary negation.

Prefix negation has the same precedence as the infix operator - defined in the Prelude (see Table 4.1 ). Because e1-e2 parses as an infix application of the binary operator -, one must write e1(-e2) for the alternative parsing. Similarly, (-) is syntax for (\ x y -> x-y), as with any infix operator, and does not denote (\ x -> -x)— one must use negate for that.

And section 3.5 concludes (again, emphasis mine):

Because - is treated specially in the grammar, (- exp) is not a section, but an application of prefix negation, as described in the preceding section. However, there is a subtract function defined in the Prelude such that (subtract exp) is equivalent to the disallowed section. The expression (+ (- exp)) can serve the same purpose.

这篇关于为什么`-`(减号)不适用于操作员部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆