Clojure:
1:13 user=> (first (conj '(1 2 3) 4))
4
1:14 user=> (first (conj [1 2 3] 4))
1
; . . .
1:17 user=> (first (conj (seq [1 2 3]) 4))
4
I understand what is going on, but should this work differently?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Documentation for
conj(from clojure.org):It’s more efficient to “add” elements to the end of a vector, while it’s more efficient to do so at the beginning of lists.
conjuses whatever is the most efficient for the data structure you give it.In the examples you give,
'(1 2 3)and(seq [1 2 3])both implementISeq(see documentation forseq?), while[1 2 3]doesn’t.Clojure’s
conjultimately calls theconsmethod (not to be confused with theconsfunction – this method is internal clojure code) on the underlying data structure; for vectors (PersistentVector),consadds elements to the end, while for lists they’re added to the front (theconsmethod forPersistentLists returns a new list with the new element as its head, and the existing list as its tail).