Flow Control
Clojure has very few flow control forms. In this section, you’ll meet if, do, and loop/recur. As it turns out, this is almost all you’ll ever need. Clojure provides a library of additional forms, but they’re largely built from these primitives.
Branch with if
Clojure’s if evaluates its first argument. If the argument is logically true, it returns the result of evaluating its second argument:
| (defn is-small? [number] |
| (if (< number 100) "yes")) |
| (is-small? 50) |
| -> "yes" |
If the first argument to if is logically false, it returns nil:
| (is-small? 50000) |
| -> nil |
If you want to define a result for the “else” part of if, add it as a third argument:
| (defn is-small? [number] ... |
Get Programming Clojure, 3rd Edition 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.