Thomson
Thomson

Reputation: 21625

What's the relationship between vector and list in Lisp?

I was told that there are only atom and list as the basic data structure in Lisp, does this mean vector in Lisp is some type of list? Was vector stored as list in the underlying?

Upvotes: 4

Views: 929

Answers (2)

Ken
Ken

Reputation: 486

What you were told was accurate but might not have been the clearest description.

In Common Lisp, at least:

* (type-of #(3 4 5))
(SIMPLE-VECTOR 3)

* (atom #(3 4 5))
T

An atom is defined as anything which is not a CONS cell -- including vectors, class instances, and so on. So yes, a vector is officially considered an "atom" in Lisp, which is why you were told what you were told.

Upvotes: 4

Andrey
Andrey

Reputation: 60065

As everywhere else vector is structure for efficient random access, usually wrapper around array with some extras (auto grow). List is best for sequentical access and fast insertion.

Upvotes: 2

Related Questions