Vectors As Sequences

As mentioned earlier, vectors and lists are the two concrete subtypes of the abstract type sequence. All the functions I’ll discuss in the next few sections are sequence functions; in addition to being applicable to vectors—both general and specialized—they can also be used with lists.

The two most basic sequence functions are **LENGTH**, which returns the length of a sequence, and **ELT**, which allows you to access individual elements via an integer index. **LENGTH** takes a sequence as its only argument and returns the number of elements it contains. For vectors with a fill pointer, this will be the value of the fill pointer. **ELT**, short for element, takes a sequence and an integer index between zero (inclusive) and the length of the sequence (exclusive) and returns the corresponding element. **ELT** will signal an error if the index is out of bounds. Like **LENGTH**, **ELT** treats a vector with a fill pointer as having the length specified by the fill pointer.

  1. (defparameter *x* (vector 1 2 3))
  2. (length *x*) ==> 3
  3. (elt *x* 0) ==> 1
  4. (elt *x* 1) ==> 2
  5. (elt *x* 2) ==> 3
  6. (elt *x* 3) ==> error

**ELT** is also a **SETF**able place, so you can set the value of a particular element like this:

  1. (setf (elt *x* 0) 10)
  2. *x* ==> #(10 2 3)