在clojure中,如何根据defmacro本身定义defmacro?
我一直在查看defmacro的源代码,它在定义中使用了“let”:在clojure中,如何根据defmacro本身定义defmacro?,clojure,Clojure,我一直在查看defmacro的源代码,它在定义中使用了“let”: (def ^{:doc "Like defn, but the resulting function name is declared as a macro and will be used as a macro by the compiler when it is called." :arglists '([name doc-string? attr-map? [params*] body]
(def
^{:doc "Like defn, but the resulting function name is declared as a
macro and will be used as a macro by the compiler when it is
called."
:arglists '([name doc-string? attr-map? [params*] body]
[name doc-string? attr-map? ([params*] body)+ attr-map?])
:added "1.0"}
defmacro (fn [&form &env
name & args]
(let [prefix (loop [p (list name) args args]
但是,“let”定义为宏本身:
(defmacro let
"binding => binding-form init-expr
Evaluates the exprs in a lexical context in which the symbols in
the binding-forms are bound to their respective init-exprs or parts
therein."
{:added "1.0", :special-form true, :forms '[(let [bindings*] exprs*)]}
[bindings & body]
(assert-args
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
`(let* ~(destructure bindings) ~@body))
有人能解释一下这是如何工作的吗?因为我不明白“defmacro”是如何定义的,因为需要定义“defmacro”。(如果这有意义:)递归宏工作正常,在clojure语言核心和其他程序的许多地方都会出现。宏只是返回S表达式的函数,因此它们可以像函数一样递归。在您的示例中,在
let
的情况下,它实际上正在校准let*
,这是一个不同的函数(函数名中有*很好),因此尽管递归宏很好,这不是它们的一个例子,这是可能的,因为在core.clj中定义defmacro
函数之前,已经有了let
at的定义(稍后会重新定义)。宏只是普通函数,它们绑定到的变量具有元数据键:macro
和valuetrue
,因此编译器可以在编译时区分宏(在编译时执行)和函数,没有这个元键,就无法区分宏和函数,因为宏本身就是一个处理S表达式的函数。否:(fn*let[&form&env&decl](cons'let*decl))
。。这取决于什么?它定义了let函数。它使用clojureIsn的java代码中已经定义的let*,这不是关于明显的循环定义的问题吗?