Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between +1 and -1

> :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?

like image 590
fredoverflow Avatar asked Mar 06 '11 11:03

fredoverflow


1 Answers

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.

like image 126
HaskellElephant Avatar answered Sep 18 '22 13:09

HaskellElephant