在「Programming Clojure(Stuart)」一書中,當閱讀宏如何擴展時,我感到困惑。如何在clojure中擴展宏?
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 [email protected]) and#))))
爲什麼鏈宏觀擴張一路,但和不是?爲什麼不擴大到類似以下內容:
user=> (macroexpand '(chain a b c d))
(. (chain a b c) d)