how is a macro expanded in clojure?
Posted
by
john wang
on Stack Overflow
See other posts from Stack Overflow
or by john wang
Published on 2012-07-04T02:59:41Z
Indexed on
2012/07/04
3:15 UTC
Read the original article
Hit count: 184
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)
© Stack Overflow or respective owner