在clojure语言中真正的是什么

use*_*423 5 macros symbols clojure cons

实际上我想要完全理解clojure,特别是符号

(def a 1)
(type a)
;;=>java.lang.Long
(type 'a)
;;=>clojure.lang.Symbol
Run Code Online (Sandbox Code Playgroud)

我知道这type是一个函数,所以它的参数首先被评估,所以我完全理解为什么上面的代码以这种方式工作.在流动的代码中,我决定使用宏来延迟评估

 (defmacro m-type [x] (type x))
 (m-type a)
 ;;==>clojure.lang.Symbol
Run Code Online (Sandbox Code Playgroud)

我很好,但我不能理解的是:

 (m-type 'a)
 ;;=>clojure.lang.Cons
Run Code Online (Sandbox Code Playgroud)

为什么'a的类型是缺点

Art*_*ldt 6

字符"由Clojure的读者作为一个读者宏它扩展到包含符号列表解释quote之后无论遵循",所以你要调用(m-type 'a)'a是扩展到:

user> (macroexpand-1 ''a)
(quote a) 
Run Code Online (Sandbox Code Playgroud)

然后调用列表上的类型(quote a)是一个缺点.

如果我们让m-type宏在它评估时看到它们的参数,那么这可能会更清楚一些:

user> (defmacro m-type [x] (println "x is " x) (type x))
#'user/m-type
user> (m-type 'a)
x is  (quote a)
clojure.lang.Cons  
Run Code Online (Sandbox Code Playgroud)

  • 我不是OP,我明白了 (2认同)