Clojure中的'()和(list)之间有什么区别?
列表上的 API Cheatsheet 部分似乎表明'()
是一个列表构造函数,就像(list)
,但我发现在实践中他们不完全一样。例如,给定:
The API Cheatsheet section on Lists seems to indicate that '()
is a list constructor, just like (list)
, but I've found that in practice they're not exactly the same. For example, given:
(def foo "a")
(def bar "b")
(def zip "c")
以下语句:
(apply str '(foo bar zip))
$ b
produces the output "foobarzip", which I wouldn't expect.
但是假设的是等价的:
(apply str (list foo bar zip))
产生abc,如我所料。
produces "abc", as I'd expect.
这是怎么回事?如果在Clojure中有一个列表的简写(例如 {}
表示映射, []
表示向量)是什么?
What's going on here? If there's a "shorthand" for a list in Clojure (like {}
for maps and []
for vectors), what is it?
在lisps中,'
换句话说'(foo bar zip)
创建一个包含符号 foo
, bar
, zip
; (list foo bar zip)
创建一个包含 foo
, bar , zip
。在第一种情况下, str
会将符号本身转换为字符串,然后将它们连接起来。
To put it another way '(foo bar zip)
creates a list containing the symbols foo
, bar
, zip
; while (list foo bar zip)
creates a list containing the values of foo
, bar
, zip
. In the first case, str
will be converting the symbols themselves to strings and then concatenating them.
:
=> (def foo "a")
=> (type (first '(foo)))
clojure.lang.Symbol
=> (type (first (list foo)))
java.lang.String