February 2018
Intermediate to advanced
304 pages
7h 11m
English
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.
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] ... |
Read now
Unlock full access