3

I was trying some Clojure, but now puzzled about the behavior of the "conj". See the exaples below:

user=> (conj [1 2 3] 4)
[1 2 3 4]

Above is expected. But now, if I do the below:

user=> (conj (reverse [1 2 3]) 4)
(4 3 2 1)

It returns (4 3 2 1). But I guess it should have returned (3 2 1 4). So, what am I missing here?

Nipun Talukdar
  • 4,975
  • 6
  • 30
  • 42
  • 1
    possible duplicate of [Inconsistency with Clojure's sequences?](http://stackoverflow.com/questions/7437833/inconsistency-with-clojures-sequences) – amalloy Apr 04 '14 at 07:59

1 Answers1

7

reverse returns a list.

(reverse [1 2 3])
=> (3 2 1)

conj has the behavior of adding something to a collection as cheaply as possible. For vectors, it'd be appending. For lists, it'd be pre-pending.

For example:

(conj '(1 2 3) 4)
=> (4 1 2 3)
Thumbnail
  • 13,293
  • 2
  • 29
  • 37
deadghost
  • 5,017
  • 3
  • 34
  • 46
  • From the docs: http://clojuredocs.org/clojure_core/clojure.core/conj "The 'addition' may happen at different 'places' depending on the concrete type." – Jason Sperske Apr 04 '14 at 03:32