Next: , Previous: , Up: Sequences Arrays Vectors   [Contents][Index]


12.4 Vectors

Arrays in Lisp, like arrays in most languages, are blocks of memory whose elements can be accessed in constant time. A vector is a general-purpose array; its elements can be any Lisp objects. (The other kind of array in SXEmacs Lisp is the string, whose elements must be characters.) Vectors in SXEmacs serve as obarrays (vectors of symbols), although this is a shortcoming that should be fixed. They are also used internally as part of the representation of a byte-compiled function; if you print such a function, you will see a vector in it.

In SXEmacs Lisp, the indices of the elements of a vector start from zero and count up from there.

Vectors are printed with square brackets surrounding the elements. Thus, a vector whose elements are the symbols a, b and a is printed as [a b a]. You can write vectors in the same way in Lisp input.

A vector, like a string or a number, is considered a constant for evaluation: the result of evaluating it is the same vector. This does not evaluate or even examine the elements of the vector. See Self-Evaluating Forms.

Here are examples of these principles:

(setq avector [1 two '(three) "four" [five]])
     ⇒ [1 two (quote (three)) "four" [five]]
(eval avector)
     ⇒ [1 two (quote (three)) "four" [five]]
(eq avector (eval avector))
     ⇒ t