+1和-1之间的差异 [英] The difference between +1 and -1

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

问题描述

 > :t(+1)
(+1):: Num a => a - > a

> :t(-1)
(-1):: Num a => a

第二个函数不是函数吗?我必须写(+( - 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之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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