🎓 A1Learner
🔍
0 of 54 lessons completed

Haskell Arithmetic

Arithmetic is working out number results with operators such as plus and times. An operator is a symbol that combines values into a new value.

Multiplication works out a product

The * operator multiplies the two numbers. The print action shows the product.

main :: IO ()
main = print (6 * 7)

Division can give a decimal result

The / operator divides two Double values. The result keeps its decimal part.

main :: IO ()
main = print (7.0 / 2)

div gives the whole part of a division

The prefix form div 17 5 divides and drops the remainder. Only the whole part remains.

main :: IO ()
main = print (div 17 5)

Power raises a number

The ^ operator raises 3 to the power 3. That means 3 times 3 times 3.

main :: IO ()
main = print (3 ^ 3)

Example

main :: IO ()
main = print ((5 + 3) * (10 - 4))

Key points

  • Addition: the + operator adds two numbers.
  • Subtraction: the - operator takes one number away from another.
  • Multiplication: the * operator multiplies two numbers.
  • Division: the / operator divides Double values and can give a decimal result.
  • div and mod: whole-number division and its remainder, written as div 7 2 or 7 `div` 2.
  • Power: the ^ operator raises a number to a whole power, so 2 ^ 10 is 1024.

In this example, the brackets group the addition and the subtraction first. Their results, 8 and 6, are then multiplied to give 48.

Note

/ needs decimal values, so write 7.0 / 2 rather than 7 / 2 for a decimal answer.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Haskell Playground

Exercise

What is the value of 17 `mod` 5?