为什么这些Clojure列表不同?

为什么这些Clojure列表不同?,clojure,Clojure,我正在处理一些问题,并在一些代码中遇到了一些奇怪的行为。经进一步调查,罪犯似乎在使用quote macro vs list函数。为什么这在下面的代码中很重要,为什么它会产生不正确的结果 user=> (= (class '(/ 1 2)) (class (list / 1 2))) true user=> (def a '(/ 1 2)) #'user/a user=> (def b (list / 1 2)) #'user/b user=> (class a) cloj

我正在处理一些问题,并在一些代码中遇到了一些奇怪的行为。经进一步调查,罪犯似乎在使用quote macro vs list函数。为什么这在下面的代码中很重要,为什么它会产生不正确的结果

user=> (= (class '(/ 1 2)) (class (list / 1 2)))
true
user=> (def a '(/ 1 2))
#'user/a
user=> (def b (list / 1 2))
#'user/b
user=> (class a)
clojure.lang.PersistentList
user=> (class b)
clojure.lang.PersistentList
user=> (apply (first a) (rest a))
2
user=> (apply (first b) (rest b))
1/2
user=> (class (first a))
clojure.lang.Symbol
user=> (class (first b))
clojure.core$_SLASH_
类似于:

(list '/ 1 2)

当您不引用
/
时,您会得到它的值,这是内置的除法函数,而不是符号。

不幸的是,您在表达式
中使用了符号对象作为函数(apply(first a)(rest a))
。符号对象在地图中查找自身作为键的值:

('/ {'+ :plus '/ :slash '- :minus} :not-found)
=> :slash

('/ {'+ :plus '$ :dollar '- :minus} :not-found)
=> :not-found

('/ 1 :not-found)
=> :not-found

('/ 1 2)
=> 2
('/ {'+ :plus '/ :slash '- :minus} :not-found)
=> :slash

('/ {'+ :plus '$ :dollar '- :minus} :not-found)
=> :not-found

('/ 1 :not-found)
=> :not-found

('/ 1 2)
=> 2