Clojure: seq (cons) vs. list (conj)
- by dbyrne
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?