Common Lisp the Language, 2nd Edition

Next: Array Information Up: Arrays Previous: Array Creation

# 17.2. Array Access

The function `aref` is normally used for accessing an element of an array. Other access functions, such as `svref`, `char`, and `bit`, may be more efficient in specialized circumstances.

[Function]
`aref array &rest subscripts`

This accesses and returns the element of array specified by the subscripts. The number of subscripts must equal the rank of the array, and each subscript must be a non-negative integer less than the corresponding array dimension.

`aref` is unusual among the functions that operate on arrays in that it completely ignores fill pointers. `aref` can access without error any array element, whether active or not. The generic sequence function `elt`, however, observes the fill pointer; accessing an element beyond the fill pointer with `elt` is an error.

Note that this remark, predating the design of the Common Lisp Object System, uses the term ``generic'' in a generic sense and not necessarily in the technical sense used by CLOS (see chapter 2).

`setf` may be used with `aref` to destructively replace an array element with a new value.

Under some circumstances it is desirable to write code that will extract an element from an array `a` given a list `z` of the indices, in such a way that the code works regardless of the rank of the array. This is easy using `apply`:

    (apply #'aref a z)

(The length of the list must of course equal the rank of the array.) This construction may be used with `setf` to alter the element so selected to some new value `w`:

    (setf (apply #'aref a z) w)

[Function]
`svref simple-vector index`

The first argument must be a simple general vector, that is, an object of type `simple-vector`. The element of the simple-vector specified by the integer index is returned.

The index must be non-negative and less than the length of the vector.

`setf` may be used with `svref` to destructively replace a simple-vector element with a new value.

`svref` is identical to `aref` except that it requires its first argument to be a simple vector. In some implementations of Common Lisp, `svref` may be faster than `aref` in situations where it is applicable. See also `schar` and `sbit`.

Next: Array Information Up: Arrays Previous: Array Creation