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

Clojure, lesson 3

Reading input and arithmetic

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

Reading a line

(read-line) reads one line and returns it as a string, without the new line at the end. Each call reads the next line. When there is nothing left to read, it returns nil.

Text to numbers

Input always arrives as text. parse-long turns a string into a whole number and parse-double into a decimal number. If the text is not a valid number, they return nil instead of crashing.

Because spaces make parsing fail, it is a good habit to trim the line first. The trimming function lives in the clojure.string library, which you load with and give the short alias :

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.

require
str

inc adds one and dec subtracts one, two tiny functions you'll use often.

Arithmetic

Operators are ordinary functions, so they come first, like every other call:

There is no precedence to memorise: the parentheses already say what happens first.

Division is where Clojure surprises newcomers. / on two integers gives an exact ratio, not a rounded number:

For whole-number problems, use quot and rem (or mod). For measurements, use decimals. (double 7/2) converts a ratio to 3.5 if you need one.

Formatting numbers

format builds a string from a format pattern, the same way printf prints one. %.1f rounds a decimal to one place, %d prints a whole number:

Further reading

  • Learn Clojure: Syntax
  • Data Structures: Numbers
  • Clojure cheatsheet

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 [name (read-line)]
  (println (str "Hi, " name "!")))
(parse-long "42")        ; 42
(parse-double "1.75")    ; 1.75
(parse-long "12abc")     ; nil
(parse-long " 7")        ; nil (spaces are not allowed)
(require '[clojure.string :as str])

(let [age (parse-long (str/trim (read-line)))]
  (println "Next year you will be" (inc age)))
(+ 1 2)          ; 3
(- 10 4)         ; 6
(* 2 3 4)        ; 24   (most operators take any number of arguments)
(+ 1 (* 2 3))    ; 7    (nesting replaces precedence rules)
(/ 7 2)          ; 7/2
(/ 8 2)          ; 4
(/ 7.0 2)        ; 3.5   (a decimal on either side gives a decimal)
(quot 17 5)      ; 3     (whole-number division)
(rem 17 5)       ; 2     (remainder)
(mod -7 2)       ; 1     (like rem, but never negative for a positive divisor)
(println (format "%.1f km" 42.195))     ; 42.2 km
5
3
Slices each: 4
Left over: 4
36.6
97.9