Lists versus Vectors

So if vectors and lists are both ordered, sequential data structures, why have both? The reason Clojure includes both vectors and lists is that while they are similar on the outside, internally these two data structures are very different. As shown in the next figure, you can think of a vector as similar to an array, a big chunk of continuous memory. Just put the first item in the first slot of your block of memory, the second item in the second, and so on.

images/vector.png

Lists, by contrast, are implemented as linked lists—hence the name. As illustrated in the figure, you can think of a list as a series of two-slot objects.

One slot contains ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.