本文介绍了+1和-1之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

这是因为( - 1)被解释为负数,但是(+ 1)被解释为curried函数(\ x-> 1 + x)。

在haskell中, (a **)是(**)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之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:50