The Fundamental If

Clojure includes an if expression, and the good news is that there isn’t a lot to say about it: Clojure’s if is about as boring a programming-language feature as you are likely to come across. An if expression starts with the word if, which is followed by two other expressions: first a condition, then an expression to evaluate if the condition is true. The whole if expression is wrapped in round parentheses:

 (​defn​ print-greeting [preferred-customer]
  (​if​ preferred-customer
  (println ​"Welcome back to Blotts Books!"​)))

Call the function in that example with true, and you will get a warm greeting printed. Call it with false, and you will get silence. You can also add an optional third expression inside ...

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.