Infix operators
Prelude
Section titled “Prelude”Logical
Section titled “Logical”&& is logical AND, || is logical OR.
== is equality, /= non-equality, < / <= lesser and > / >= greater operators.
Arithmetic operators
Section titled “Arithmetic operators”The numerical operators +, - and / behave largely as you’d expect. (Division works only on fractional numbers to avoid rounding issues – integer division must be done with quot or div). More unusual are Haskell’s three exponentiation operators:
4^5 ≡ (4*4)*(4*4)*43^^(-2) ≡ 1 / (2*2)Unlike ^, this requires a fractional base type (i.e. 4^^5 :: Int will not work, only 4^5 :: Int or 4^^5 :: Rational).
2**pi ≡ exp (pi * log 2)There are two concatenation operators:
[1,2] ++ [3,4] ≡ 1 : 2 : [3,4] ≡ 1 : [2,3,4] ≡ [1,2,3,4]!! is an indexing operator.
[0, 10, 20, 30, 40] !! 3 ≡ 30Note that indexing lists is inefficient (complexity O(n) instead of O(1) for arrays or O(log n) for maps); it’s generally preferred in Haskell to deconstruct lists by folding ot pattern matching instead of indexing.
Control flow
Section titled “Control flow”f $ x ≡ f x ≡ f(x) -- disapproved styleThis operator is mostly used to avoid parentheses. It also has a strict version $!, which forces the argument to be evaluated before applying the function.
(f . g) x ≡ f (g x) ≡ f $ g xFinding information about infix operators
Section titled “Finding information about infix operators”Because infixes are so common in Haskell, you will regularly need to look up their signature etc.. Fortunately, this is just as easy as for any other function:
Prelude> :i +class Num a where (+) :: a -> a -> a ... -- Defined in ‘GHC.Num’infixl 6 +Prelude> :i ^^(^^) :: (Fractional a, Integral b) => a -> b -> a -- Defined in ‘GHC.Real’infixr 8 ^^This tells me that ^^ binds more tightly than +, both take numerical types as their elements, but ^^ requires the exponent to be integral and the base to be fractional.
The less verbose :t requires the operator in parentheses, like
Prelude> :t (==)(==) :: Eq a => a -> a -> BoolCustom operators
Section titled “Custom operators”In Haskell, you can define any infix operator you like. For example, I could define the list-enveloping operator as
(>+<) :: [a] -> [a] -> [a]env >+< l = env ++ l ++ env
GHCi> "**">+<"emphasis""**emphasis**"You should always give such operators a fixity declaration, like
infixr 5 >+<(which would mean >+< binds as tightly as ++ and : do).
Remarks
Section titled “Remarks”Most Haskell functions are called with the function name followed by arguments (prefix notation). For functions that accept two arguments like (+), it sometimes makes sense to provide an argument before and after the function (infix).