Clojure: seq (cons) vs. list (conj)
Posted
by dbyrne
on Stack Overflow
See other posts from Stack Overflow
or by dbyrne
Published on 2010-06-09T17:50:33Z
Indexed on
2010/06/09
17:52 UTC
Read the original article
Hit count: 304
I know that cons
returns a seq and conj
returns a collection. I also know that conj
"adds" the item to the optimal end of the collection, and cons
always "adds" the item to the front. This example illustrates both of these points:
user=> (conj [1 2 3] 4) //returns a collection
[1 2 3 4]
user=> (cons 4 [1 2 3]) //returns a seq
(4 1 2 3)
For vectors, maps, and sets these differences make sense to me. However, for lists they seem identical.
user=> (conj '(3 2 1) 4)
(4 3 2 1)
user=> (cons 4 '(3 2 1))
(4 3 2 1)
Are there any examples using lists where conj
vs. cons
exhibit different behaviors, or are they truly interchangeable? Phrased differently, is there an example where a list and a seq cannot be used equivalently?
© Stack Overflow or respective owner