Hi Nipra,

2009/11/27 nipra <prabhakar.nik...@gmail.com>:
> Hi,
>  (rest *v1) is equal to *v2 in the below mentioned example. Then why
>  `conj' operation on them is returning different things.
>
> user> (def *v1 [1 2 3 4 5])
> #'user/*v1
> user> (def *v2 [2 3 4 5])
> #'user/*v2
> user> (= (rest *v1) *v2)
> true
> user> (def *v3 (conj (rest *v1) 0))
> #'user/*v3
> user> *v3
> (0 2 3 4 5)
> user> (def *v4 (conj *v2 0))
> #'user/*v4
> user> *v4
> [2 3 4 5 0]
> user> (map #(class %) (list *v1 *v2 *v3 *v4))
> (clojure.lang.PersistentVector clojure.lang.PersistentVector
> clojure.lang.Cons clojure.lang.PersistentVector)
> user>

(rest) returns a seq rather than a vector:

user> (class (rest *v1))
clojure.lang.PersistentVector$ChunkedSeq

and (conj) does different thing on seqs and vectors:

user> (conj '(1 2 3) 0)
(0 1 2 3)
user> (conj [1 2 3] 0)
[1 2 3 0]

namely, conj on a vector appends the new element to the vector, and
conj on a seq (or a list) prepends the new element.

So, in effect you are testing the equality of (0 2 3 4 5) and [2 3 4 5
0] which are not the same, as you found out.

Finally, the equality check on (rest *v1) and *v2 succeeds, because
(=) treats both (rest *v1) and *v2 as seqs and traverses both checking
each element for equality, i.e. it does not care that the containers
are of different types.

> Regards,
> nipra

-- 
  ! Lauri

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to