为什么 `-`(减号)对运算符部分不起作用?
Why doesn't `-` (minus) work for operator sections?
根据位置,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
<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
本例中如何解决这个问题得到7
?
使用subtract
。 -
是 Haskell 中唯一的运算符,它同时出现在前缀 和 二进制中缀变体中:
let a = -3 -- prefix variant
let b = (-3) -- also prefix variant!
let c = 4 - 3 -- binary variant
因此,您将不得不使用 (subtract 3) 10
。另见 section 3.4 in the Haskell 2010 report(强调我的):
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.
并且 section 3.5 总结(再次强调我的):
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.
根据位置,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
<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
本例中如何解决这个问题得到7
?
使用subtract
。 -
是 Haskell 中唯一的运算符,它同时出现在前缀 和 二进制中缀变体中:
let a = -3 -- prefix variant
let b = (-3) -- also prefix variant!
let c = 4 - 3 -- binary variant
因此,您将不得不使用 (subtract 3) 10
。另见 section 3.4 in the Haskell 2010 report(强调我的):
The special form
-e
denotes prefix negation, the only prefix operator in Haskell, and is syntax fornegate (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 thenegate
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 ). Becausee1-e2
parses as an infix application of the binary operator-
, one must writee1(-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 usenegate
for that.
并且 section 3.5 总结(再次强调我的):
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 asubtract
function defined in the Prelude such that(subtract exp)
is equivalent to the disallowed section. The expression(+ (- exp))
can serve the same purpose.