Education‎ > ‎Closer to Clojure‎ > ‎

Closer to Clojure 10: Macro

Macro of Clojure is a mixture of high-level macro (syntax-rules) of Scheme and low-level macro of Common LISP. You can use pattern matching and also natural recursion in macro definition.

(defmacro and
  ([] true)
  ([x] x)
  ([x & rest]
    `(let [and# ~x]
      (if and#
        (and [email protected])
        and#))))

Symbols ended with # are passed to gensym mechanism to avoid name collision. Commas are just white spaces in Clojure, you need to use tilde instead of commas to unquote.

Reference:
http://clojure.org/lisp
マクロはSchemeの高レベルマクロ (syntax-rules) と CommonLISP の低レベルマクロを組み合わせたような感じです.マクロはパタンマッチが使えますし,自然な再帰もできます.

(defmacro and
  ([] true)
  ([x] x)
  ([x & rest]
    `(let [and# ~x]
      (if and#
        (and [email protected])
        and#))))

#で終わるシンボルは名前の衝突を避けるためのgensymが自動的に行われます.カンマはClojureでは空白の意味なので,アンクォートにはチルダ文字が割り当てられています.

参考
http://clojure.org/lisp
Comments