Arithmetic

Another thing that usually comes early in learning a programming language is figuring out how to do basic arithmetic. Clojure’s approach to doing math is refreshingly—and perhaps a little disconcertingly—simple. To see what this means, let’s add a couple of numbers together:

 (+ 1900 84)

Run the expression in that example through the REPL and you will get back 1984. You do multiplication, subtraction, and division in the same way:

 (* 16 124) ​; Gives you 1984.
 (- 2000 16) ​; 1984 again.
 (/ 25792 13) ​; 1984 yet again!

As you might expect, you can assemble these basic math operations into arbitrarily complex expressions. Thus you can get the average of 1984 and 2010 with this:

 (/ (+ 1984 2010) 2)

Arithmetic in Clojure can be ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.