clojure 列表操作结果不相等
clojure list operation result not equal
我测试了一些列表操作,发现了两种语法的区别。
(conj (cons 321321 [1]) 123123123)
=> (123123123 321321 1)
和
(cons 321321 [1])
=> (321321 1)
(conj [321312 1] 123123123)
=> [321312 1 123123123]
为什么这些结果不相等?
因为你们在做不同的事情。
cons
http://clojuredocs.org/clojure.core/cons
Returns a new seq where x is the first element and seq is
the rest.
conj
http://clojuredocs.org/clojure.core/conj
Returns a new collection with the xs
'added'. (conj nil item) returns (item). The 'addition' may
happen at different 'places' depending on the concrete type.
在您的第一个示例中,您是 "prepending" 一个新条目(将 conj 添加到序列的最简单方法),在您的第二个示例中,您是 "appending" 一个向量(同样是最简单的方法conj 添加)。
user=> (.getClass (cons 321321 [1]))
clojure.lang.Cons
user=> (.getClass (conj (cons 321321 [1]) 123123123))
clojure.lang.Cons
请注意您接下来要使用 [...]
!
user=> (.getClass [321312 1])
clojure.lang.PersistentVector
user=> (.getClass (conj [321312 1] 123123123))
clojure.lang.PersistentVector
我测试了一些列表操作,发现了两种语法的区别。
(conj (cons 321321 [1]) 123123123)
=> (123123123 321321 1)
和
(cons 321321 [1])
=> (321321 1)
(conj [321312 1] 123123123)
=> [321312 1 123123123]
为什么这些结果不相等?
因为你们在做不同的事情。
cons
http://clojuredocs.org/clojure.core/cons
Returns a new seq where x is the first element and seq is the rest.
conj
http://clojuredocs.org/clojure.core/conj
Returns a new collection with the xs 'added'. (conj nil item) returns (item). The 'addition' may happen at different 'places' depending on the concrete type.
在您的第一个示例中,您是 "prepending" 一个新条目(将 conj 添加到序列的最简单方法),在您的第二个示例中,您是 "appending" 一个向量(同样是最简单的方法conj 添加)。
user=> (.getClass (cons 321321 [1]))
clojure.lang.Cons
user=> (.getClass (conj (cons 321321 [1]) 123123123))
clojure.lang.Cons
请注意您接下来要使用 [...]
!
user=> (.getClass [321312 1])
clojure.lang.PersistentVector
user=> (.getClass (conj [321312 1] 123123123))
clojure.lang.PersistentVector