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

OCaml, lesson 3

Reading input and arithmetic

Programs become useful when they react to data. The simplest source of data is standard input (stdin): the text typed by a user or, here, supplied by the tests.

Reading a line

read_line () reads one line of input and gives it back as a string, without the trailing new line. The () is the unit value: read_line needs no real argument, but every OCaml function call must pass something, so you pass unit.

Each call reads the next line, so two calls read two lines. If there is no line left, read_line raises the End_of_file exception and the program stops.

Text to numbers

Input always arrives as text. To calculate, convert it:

int_of_string "42" is . If the text is not a number, it raises an exception. Stray spaces also count as "not a number", so is useful when input might have them: .

2 exercises to try what you just read.

Exercise 1

Pizza party

Press Ctrl Enter or Check to test your code.

Exercise 2

Temperature converter

Press Ctrl Enter or Check to test your code.

42
String.trim
int_of_string (String.trim line)

You will often see the pipe operator |> used for this. x |> f means f x, so you can read a chain of steps from left to right:

Integer arithmetic

The operators on int are +, -, *, / and mod. Division between integers throws away the fractional part, and mod gives the remainder:

Usual precedence applies: *, / and mod bind tighter than + and -. Use parentheses whenever it makes the meaning clearer.

Float arithmetic

Floats have their own operators with a dot: +., -., *., /.. Float literals need a decimal point too, even for whole numbers: 2. or 2.0, not 2.

Why two sets of operators? OCaml refuses to guess. With separate operators, the compiler always knows which kind of number you mean, and it can tell you immediately if you mix them up. To combine an int with a float, convert one of them: float_of_int 3 *. 1.5.

In a Printf format, %.1f prints a float rounded to one decimal place, %.2f to two, and so on.

Further reading

  • Basic Data Types and Pattern Matching
  • Module Stdlib (read_line, int_of_string, operators)

Every pizza has 8 slices. Read the number of people (first line) and the number of pizzas (second line). Share the slices equally, then print how many whole slices each person gets and how many are left over. There is always at least one person.

Sample input:

Sample output:

Read a temperature in degrees Celsius (it may have a decimal part) and print it in Fahrenheit, rounded to one decimal place. The formula is F = C * 9 / 5 + 32.

Sample input:

Sample output:

let () =
  let name = read_line () in
  print_endline ("Hi, " ^ name ^ "!")
let () =
  let age = int_of_string (read_line ()) in
  let height = float_of_string (read_line ()) in
  Printf.printf "%d %.2f\n" (age + 1) (height *. 100.)
let count = read_line () |> String.trim |> int_of_string
let () =
  Printf.printf "%d %d\n" (17 / 5) (17 mod 5)   (* 3 2 *)
let () =
  let km = 42.195 in
  let miles = km /. 1.609 in
  Printf.printf "%.1f miles\n" miles        (* 26.2 miles *)
5
3
Slices each: 4
Left over: 4
36.6
97.9