April 2012
Intermediate to advanced
296 pages
7h 3m
English
Clojure provides the if special form as part of the language:
(if (= 1 1) (println "yep, math still works today")) | |
| yep, math still works today |
Some languages have an unless, which is (almost) the opposite of if. unless performs a test and then executes its body only if the test is logically false.
Clojure doesn’t have unless, but it does have an equivalent macro called when-not. For the sake of having a simple example to start with, let’s pretend that when-not doesn’t exist and create an implementation of unless. To follow the rules of Macro Club, begin by trying to write unless as a function:
| src/examples/macros.clj | |
; This is doomed to fail... | |
(defn unless [expr form] | |
(if expr nil form)) ... | |
Read now
Unlock full access