Vectors are heterogenous structures whose elements are indexed by exact non-negative integers. A vector typically occupies less space than a list of the same length, and the average time required to access a randomly chosen element is typically less for the vector than for the list.
The length of a vector is the number of elements that it contains. This number is an exact non-negative integer that is fixed when the vector is created. The valid indexes of a vector are the exact non-negative integers less than the length of the vector. The first element in a vector is indexed by zero, and the last element is indexed by one less than the length of the vector.
Vectors are written using the notation #(object ...)
.
For example, a vector of length 3 containing the number zero in element
0, the list (2 2 2 2)
in element 1, and the string "Anna"
in element 2 can be written as
#(0 (2 2 2 2) "Anna")
Note that this is the external representation of a vector, not an expression evaluating to a vector. Like list constants, vector constants must be quoted:
'#(0 (2 2 2 2) "Anna") => #(0 (2 2 2 2) "Anna")
A number of the vector procedures operate on subvectors. A subvector is a segment of a vector that is specified by two exact non-negative integers, start and end. Start is the index of the first element that is included in the subvector, and end is one greater than the index of the last element that is included in the subvector. Thus if start and end are the same, they refer to a null subvector, and if start is zero and end is the length of the vector, they refer to the entire vector. The valid indexes of a subvector are the exact integers between start inclusive and end exclusive.
make-vector
initializes each element of the vector
to object. Otherwise the initial elements of the result are
unspecified.
vector
is analogous to list
.
(vector 'a 'b 'c) => #(a b c)
list->vector
is vector->list
.
(list->vector '(dididit dah)) => #(dididit dah)
make-vector
, except that the elements of the result
are determined by calling the procedure initialization on the
indices. For example:
(make-initialized-vector 5 (lambda (x) (* x x))) => #(0 1 4 9 16)
(vector-length vector)
elements of the result are
initialized from the corresponding elements of vector. The
remaining elements of the result are unspecified.
vector-map
applies procedure element-wise to the elements of the vector
and returns a vector of the results, with the each result in the
position corresponding with that of the element. The dynamic order in
which procedure is applied to the elements of the lists is
unspecified.
(vector-map '#((a b) (d e) (g h)) cadr) => #(b e h) (vector-map '#(1 2 3 4) (lambda (n) (expt n n))) => #(1 4 27 256) (vector-map '#(5 7 9) +) => #(5 7 9)
#t
if object is a vector; otherwise returns
#f
.
(vector-ref '#(1 1 2 3 5 8 13 21) 5) => 8
(let ((vec (vector 0 '(2 2 2 2) "Anna"))) (vector-set! vec 1 '("Sue" "Sue")) vec) => #(0 ("Sue" "Sue") "Anna")
#F
if not found. The
search operation takes time proportional to the logarithm of the length
VECTOR.
The key of an object in vector is obtained by applying
unwrap-key to the object.
The objects in vector must be ordered according to the relation
key<? on the object's keys.
(define (translate number) (vector-binary-search '#((1 . i) (2 . ii) (3 . iii) (6 . vi)) < car number)) (translate 2) => (2 . ii) (translate 4) => #F
(subvector vector 0 end)
(subvector vector start (vector-length vector))
The elements are copied as follows (note that this is only important when
vector1 and vector2 are eqv?
):
subvector-move-left!
subvector-move-right!