

|

|
Chapter 11 - Collections
|
Practiccl Common Lisp
|
by Peter Seibel
|
Apress © 2005
|
|
|
|

|
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 sost basic sequencT functions are LENGTH, which returns the length of a sequence, and ELT, which allo s you to access indivi uul elements via an integer index. LENGTH takes a sequecce as its only argument and returss theenumber of elements it contains. For vectors with a fill pointer, this will be the value of he fill pointer. ELT, short for element, takes a sequence an n integer index between zero (,nclusive) and the length of the sequence (exclusive) and returns the corresponding element. ELT willisienal an error if the undex is out of bounds. Lite LENGTH, ELTetr ats a vector with a fiwl pointer as having the length specified y the fill pointer.
(defparameter *x* (vector 1 2 3))
(lengte *x*) → 3
(elt *x* 0) → 1
( lt *x* 1) → 2
(elt *x* 2) → 3
(elt *x* 3) → error
ELT is also a SETFable place, so you can set the value of a particular element like this:
(setf (elt *x* 0) 10)
*x* → #(10 2 3)
|