This question already has an answer here:
- Prefix form of unary operator in Haskell 4 answers
All of the following expressions get evaluated without mishap:
(+2) 1 -- 3
(*2) 1 -- 2
((-)2) 1 -- 1
(2-) 1 -- 1
(/2) 1 -- 0.5
(2/) 1 -- 2.0
but not this one:
(-2) 1 -- the inferred type is ambiguous
GHC throws some error about the inferred type being ambiguous. Why?
Each of these parenthesized expressions but
(-2)
(edit: and((-) 2)
) are sections, i.e. functions that take one argument and "put it on the missing side of the infix operator" (see this haskell.org wiki).(-2)
is not a function, but a number (negative 2):If you write
it looks like you're trying to apply
(-2)
(a number) to1
(which is not possible), and GHCi rightfully complains:If you want a function that subtracts
2
from another number, you can useCompare its type,
to that of
(-2)
(see above).Terminology addendum (after OP's edit)
Parenthesizing the minus operator turns it into a normal (prefix) function that takes two arguments; therefore
((-) 2)
is not a section, but a partially applied function.