views:

1074

answers:

4

I read that Vectors are not seqs, but Lists are. I'm not sure what the rationale is for using one over the other. It seems that vectors are used the most, but is there a reason for that? Any answers are appreciated, thanks!

+7  A: 

If you've done Java programming a lot, and are familiar with the Java collection framework, think of lists like LinkedList, and vectors like ArrayList. So you can pretty much choose containers the same way.

For further clarification: if you intend to add items individually to the front or the back of the sequence a lot, a linked list is much better than a vector, because the items don't need to be shuffled around each time. However, if you want to get at specific elements (not near the front or back of the list) frequently (i.e., random access), you will want to use vector.

By the way, vectors can easily be turned into seqs.

user=> (def v (vector 1 2 3))
#'user/v
user=> v
[1 2 3]
user=> (seq v)
(1 2 3)
user=> (rseq v)
(3 2 1)
Chris Jester-Young
Vectors aren't seqs, but they are sequential. (source: Rich himself on #clojure on freenode.) Also, I don't really know Java at all, but Rich did just answer my question.
Rayne
I will edit my post to say, vectors can be _made into_ seqs, via the seq function. :-)
Chris Jester-Young
Chose your answer because it did indeed answer the question, and I really don't like choosing my own answers as correct. Doesn't seem right. Thanks. :)
Rayne
+4  A: 

Vectors have O(1) random access times, but they have to be preallocated. Lists can be dynamically extended, but accessing a random element is O(n).

Svante
Technically, linked lists have O(1) access times...if you're accessing the front or back element only. :-P However, vectors do have O(1) random access. :-)
Chris Jester-Young
("Linked list" as described above refer to doubly-linked lists. Singly-linked lists have O(1) access to the front element only. :-P)
Chris Jester-Young
Thanks for making the edits! :-)
Chris Jester-Young
+9  A: 

Once again, it seems I've answered my own question by getting impatient and asking it in #clojure on Freenode. Good thing answering your own questions is encouraged on Stackoverflow.com :D

I had a quick discussion with Rich Hickey, and here is the gist of it.

[12:21] <Raynes> Vectors aren't seqs, right?
[12:21] <rhickey> Raynes: no, but they are sequential
[12:21] <rhickey> ,(sequential? [1 2 3])
[12:21] <clojurebot> true
[12:22] <Raynes> When would you want to use a list over a vector?
[12:22] <rhickey> when generating code, when generating back-to-front
[12:23] <rhickey> not too often in Clojure
Rayne
While you're on freenode, come to the dark side and join #stackoverflow! :-P
Chris Jester-Young
I actually used to idle there. I switched IRC clients and never thought to add #stackoverflow to my autojoin list.
Rayne
+4  A: 

just a quick side note:

"I read that Vectors are not seqs, but Lists are." 

sequences are more generic than either lists or vectors (or maps or sets).
Its unfortunate that the REPL prints lists and sequences the same because it really makes it look like lists are sequences even though they are different. the (seq ) function will make a sequence from a lot of different things including lists, and you can then feed that seq to any of the plethora of functions that do nifty things with seqs.

(class? (list 1 2 3))
vs. 
(class? (seq (list 1 2 3)))
Arthur Ulfeldt
I dont mean to pick on a small point, its just an opportunity to point out somthing useful. many will already know this :)
Arthur Ulfeldt