how is a macro expanded in clojure?
- by john wang
In the book Programming Clojure(Stuart), when read how macros are expanded I got confused.
user=> (defmacro chain
([x form] (list '. x form))
([x form & more] (concat (list 'chain (list '. x form)) more)))
#'user/chain
The above macro can be expanded as:
user=> (macroexpand '(chain a b c))
(. (. a b) c)
But the following is only expanded to the first level:
user=> (macroexpand '(and a b c))
(let* [and__3822__auto__ a]
(if and__3822__auto__ (clojure.core/and b c) and__3822__auto__))
The and macro source:
user=> (source and)
(defmacro and([] true)
([x] x)
([x & next]
`(let [and# ~x]
(if and# (and ~@next) and#))))
Why is the chain macro expanded all the way but the and not ? Why is it not expanded to something like the following:
user=> (macroexpand '(chain a b c d))
(. (chain a b c) d)