Learn/Elixir/3. Reading input and arithmetic
Lesson 3 of 8
Sign in

Elixir, lesson 3

Reading input and arithmetic

Programs get more interesting when they react to input. Your exercises receive their input on standard input (stdin), one line at a time, as if someone typed it.

Reading a line

IO.gets/1 reads one line. Its argument is a prompt to print first; pass an empty string "" so nothing extra appears in your output.

What comes back is the text including the line break at the end, like "42\n". If there is no more input, you get the atom :eof instead. To remove the line break (and any stray spaces), call String.trim/1:

The pipe operator

Nesting calls inside calls gets hard to read. Elixir's pipe operator |> takes the value on its left and passes it as the first argument of the function on its right. These two lines are identical:

2 exercises to try what you just read.

Exercise 1

Pizza party

Press Ctrl Enter or Check to test your code.

Exercise 2

Celsius to Fahrenheit

Press Ctrl Enter or Check to test your code.

Read the second one left to right: get a line, trim it, turn it into an integer. Pipes are one of the most loved parts of Elixir, and you will see them everywhere.

Text to numbers

Input is always text. "7" and 7 are different things, and "7" + 1 is an error.

  • String.to_integer("42") returns 42. It raises an error if the text is not a whole number.
  • Float.parse("36.6") returns a tuple {36.6, ""}: the number plus any leftover text. It accepts whole numbers too: Float.parse("37") gives {37.0, ""}.

You take the number out of the tuple with pattern matching:

The left side has the same shape as the right, so Elixir binds celsius to the number. The underscore at the start of _rest says "I don't need this part".

Arithmetic

ExpressionResultMeaning
7 + 29add
7 - 25subtract
7 * 214multiply
7 / 23.5divide, always a float
div(7, 2)3whole-number division
rem(7, 2)1remainder

Note that / gives a float even when the result is whole: 10 / 2 is 5.0. When you want whole numbers, use div/2 and rem/2. Multiplication and division happen before addition and subtraction; use parentheses when you want a different order.

To round a float, use Float.round(2.345, 2) (gives 2.35) or round(2.6) (gives the integer 3).

Further reading

  • The IO module (IO.gets/2)
  • String.to_integer/1 and Float.parse/1
  • Getting started: Basic types (arithmetic)

A group shares some pizza slices equally. Every person gets the same whole number of slices, and the rest are left over.

Input: two lines. The first is the number of slices (0 or more), the second the number of people (1 or more).

Output: two lines:

That sample is for the input:

Read a temperature in degrees Celsius and print it in Fahrenheit, using F = C * 9 / 5 + 32.

Input: one line with a number, which may have a decimal part (36.6) or not (100).

Output: just the Fahrenheit value. Any normal float form is accepted, so 212.0 and 212 both count for 100 °C.

prints

line = IO.gets("")
name = String.trim(IO.gets(""))
IO.puts("Hi, #{name}!")
age = String.to_integer(String.trim(IO.gets("")))
age = IO.gets("") |> String.trim() |> String.to_integer()
{celsius, _rest} = IO.gets("") |> String.trim() |> Float.parse()
Each person gets 3 slices.
Leftover: 2
14
4
37
98.6