[题外话] Scheme中的宏与Clojure中的宏 有区别吗??

最近发现一个问题, 在Scheme中,宏基本是这个写法

(define-syntax define-syntax-rule
  (syntax-rules ()
    ((_ (name pat ...) body ...)
     (define-syntax name
       (syntax-rules ()
	 ((_ pat ...)
	  body ...))))))

而在Clojure

(defmacro ->>
  "Threads the expr through the forms. Inserts x as the
  last item in the first form, making a list of it if it is not a
  list already. If there are more forms, inserts the first form as the
  last item in second form, etc."
  {:added "1.1"}
  [x & forms]
  (loop [x x, forms forms]
    (if forms
      (let [form (first forms)
            threaded (if (seq? form)
              (with-meta `(~(first form) ~@(next form)  ~x) (meta form))
              (list form x))]
        (recur threaded (next forms)))
      x)))

Scheme中的宏就像bash中的模式匹配,打个通配符就行
Clojure是把宏里的代码当作数据结构来操作
这两种宏有区别吗,有没有其他语言也在用类似Scheme中的宏??

可能跟底层实现有关系吧,没太研究过。。

简单的说,Clojure里的macro更类似 implicit renaming(也是一种hygienic的实现方式)

What’s implicit renaming: Mini-tutorial on explicit (and implicit) renaming macros in CHICKEN - The CHICKEN Scheme wiki

1 个赞