Unary operator

From HaskellWiki
Revision as of 10:37, 9 September 2007 by Lemming (talk | contribs) (explanation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

A unary operator is an operator with one parameter. It can be written in prefix notation like in or in postfix notation (factorial).

In Haskell there is only one unary operator, namely the unary minus. It has been discussed in length, whether the unary minus shall be part of numeric literals or whether it shall be an independent operator. In fact in Haskell it is the latter one, in order to allow expressions like -a. The unary minus is syntactic sugar for the Prelude function negate. The special syntax disallows the use of sectioning. That is, you cannot write map (-2) if you want to substract 2 from all elements of a list. Instead you have to write map (subtract 2).

See also