Asking Questions

Being able to branch on an explicit true or false is only half of what makes if the programming workhorse that it is. The other half is being able to ask the questions that evaluate to a Boolean. Probably the most common question we ask in programs is Does this thing equal this other thing? Happily, the Clojure equality-testing function has a very obvious name.

It’s just a single equals sign:

 (= 1 1) ​; True!
 
 (= 2 (+ 1 1)) ​; True again!
 
 (= ​"Anna Karenina"​ ​"Jane Eyre"​) ​; Nope.
 
 (= ​"Emma"​ ​"Emma"​) ​; Yes!

Like + and *, the = function looks like an operator but is really just a function. And like + and *, the = function will take any number of arguments:

 (= (+ 2 2) 4 (/ 40 10) (* 2 2) (- 5 1)) ​; True!

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.