+1和-1之间的差异 [英] The difference between +1 and -1
问题描述
> :t(+1)
(+1):: Num a => a - > a
> :t(-1)
(-1):: Num a => a
第二个函数不是函数吗?我必须写(+( - 1))
或者有更好的方法吗?
这是因为( - 1)
被解释为负数,但是(+ 1)
被解释为curried函数(\ x-> 1 + x)
。
在haskell中, (a **)
是的语法糖(* * a)
是(\ x - > x ** a)
。然而,( - )
是一种特殊情况,因为它既是一元运算符(否定)又是二元运算符(减号)。因此,这种语法糖在这里不能被明确地应用。当你想(\ x - > a-x)
时,你可以写( - )a
正如已经在柯里化减法中回答的那样,您可以使用函数 negate
和减去
以区分一元和二元 -
函数。
> :t (+1)
(+1) :: Num a => a -> a
> :t (-1)
(-1) :: Num a => a
How come the second one is not a function? Do I have to write (+(-1))
or is there a better way?
This is because (-1)
is interpreted as negative one, however (+1)
is interpreted as the curried function (\x->1+x)
.
In haskell, (a **)
is syntactic sugar for (**) a
, and (** a)
is (\x -> x ** a)
. However (-)
is a special case since it is both a unary operator (negate) and a binary operator (minus). Therefore this syntactic sugar cannot be applied unambiguously here. When you want (\x -> a - x)
you can write (-) a
, and, as already answered in Currying subtraction, you can use the functions negate
and subtract
to disambiguate between the unary and binary -
functions.
这篇关于+1和-1之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!