Reputation: 38
I test some list operation, find this difference with two syntax。
(conj (cons 321321 [1]) 123123123)
=> (123123123 321321 1)
and
(cons 321321 [1])
=> (321321 1)
(conj [321312 1] 123123123)
=> [321312 1 123123123]
why these result isn't equal?
Upvotes: 0
Views: 84
Reputation: 37073
Because you are doing different things.
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.
in your first example you are "prepending" a new entry (easiest way for conj to add to a sequence) and in your second example you are "appending" to a vector (again easiest way for conj to add).
user=> (.getClass (cons 321321 [1]))
clojure.lang.Cons
user=> (.getClass (conj (cons 321321 [1]) 123123123))
clojure.lang.Cons
Note you are using [...]
next!
user=> (.getClass [321312 1])
clojure.lang.PersistentVector
user=> (.getClass (conj [321312 1] 123123123))
clojure.lang.PersistentVector
Upvotes: 8