プログラミングClojure(Stuart)の本で、マクロがどのように展開されるかを読んだとき、私は混乱しました。
user=> (defmacro chain
([x form] (list '. x form))
([x form & more] (concat (list 'chain (list '. x form)) more)))
#'user/chain
上記のマクロは次のように展開できます。
user=> (macroexpand '(chain a b c))
(. (. a b) c)
ただし、以下は最初のレベルにのみ拡張されています。
user=> (macroexpand '(and a b c))
(let* [and__3822__auto__ a]
(if and__3822__auto__ (clojure.core/and b c) and__3822__auto__))
およびマクロソース:
user=> (source and)
(defmacro and([] true)
([x] x)
([x & next]
`(let [and# ~x]
(if and# (and ~@next) and#))))
チェーンマクロが完全に拡張されているのに、ではないのはなぜですか?次のようなものに拡張されないのはなぜですか。
user=> (macroexpand '(chain a b c d))
(. (chain a b c) d)