Haskell Expressions & Arithmetic
In Haskell everything is an expression: something that has a value. There are no statements that "do" things without producing a value. Even if is an expression, which is why it always needs an else.
Arithmetic
main :: IO ()
main = do
print (7 + 3)
print (7 - 10)
print (7 * 3)
print (7 / 2) -- fractional division
print (7 `div` 2) -- integer division
print (7 `mod` 2) -- remainder
print (2 ^ 10) -- power with a non-negative integer exponent
print (2 ** 0.5) -- power with a floating-point exponent10
-3
21
3.5
3
1
1024
1.4142135623730951
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * |
add, subtract, multiply | 4 * 5 |
20 |
/ |
division for fractional types (Double) |
7 / 2 |
3.5 |
`div` / `mod` |
integer division and remainder, rounding down | (-7) `div` 2 |
-4 |
`quot` / `rem` |
integer division and remainder, rounding toward zero | (-7) `quot` 2 |
-3 |
^ |
integer power | 3 ^ 4 |
81 |
** |
floating-point power | 9 ** 0.5 |
3.0 |
A named function like div written in backticks becomes an infix operator: 7 `div` 2 is the same as div 7 2. The opposite also works: wrapping an operator in parentheses makes it a normal prefix function, so (+) 3 4 is 7.
Precedence and negative numbers
The usual maths precedence applies (^ before * and /, which come before + and -), and function application binds tighter than any operator:
main :: IO ()
main = do
print (2 + 3 * 4)
print ((2 + 3) * 4)
print (abs (-5)) -- negative literals need parentheses as arguments
print (negate 8)
print (max 3 9 + 1) -- (max 3 9) + 1
print (min 2.5 1.5)
print ((-7) `div` 2, (-7) `quot` 2)14
20
5
-8
10
1.5
(-4,-3)
Note: Writing
abs -5is a common mistake: Haskell reads it asabs - 5, "abs minus five", and reports a type error. Put negative numbers in parentheses when you pass them to a function.
Comparison and Booleans
main :: IO ()
main = do
print (3 < 5)
print (3 == 3.0)
print (4 /= 4) -- "not equal" is /=, not !=
print (True && False)
print (True || False)
print (not True)
print (compare 2 9)True
True
False
False
True
False
LT
compare returns a value of type Ordering: LT, EQ or GT.
if is an expression
Because if … then … else … produces a value, you can use it anywhere a value is expected. Both branches must have the same type, and the else is required:
main :: IO ()
main = do
let temperature = 31
advice = if temperature > 25 then "Drink water" else "Take a jacket"
putStrLn advice
print (10 + (if temperature > 30 then 5 else 0))Drink water
15
Mixing integers and decimals
Haskell never converts numbers silently. Integer types (Int, Integer) and fractional types (Double) are different, so you convert explicitly with fromIntegral:
average :: [Int] -> Double
average xs = fromIntegral (sum xs) / fromIntegral (length xs)
main :: IO ()
main = do
print (average [4, 8, 15, 16, 23, 42])
print (round 2.5, round 3.5, truncate 2.9, ceiling 2.1, floor (-2.1))
print (0.1 + 0.2)
print (2 ^ 64)18.0
(2,4,2,3,-3)
0.30000000000000004
18446744073709551616
Some details worth knowing:
rounduses banker's rounding: halves go to the nearest even number, soround 2.5is2.0.1 + 0.2shows the usual floating-point rounding error, as in every language that uses IEEE doubles.2 ^ 64did not overflow. A number literal with no other constraint defaults toInteger, which has unlimited size. The fixed-sizeInt(64-bit) is faster but wraps around.
Test yourself
What is div (-7) 2?
- -3
- -4
- -3.5
- 3
Why does abs -5 fail to compile?
absdoes not exist- It is parsed as
abs - 5(subtraction) - Negative numbers are not allowed
absneeds two arguments
Which operator means "not equal" in Haskell?
!=<>/==/=