Go to the first, previous, next, last section, table of contents.
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 vector and
returns a newly allocated vector of the results, in order from left to
right. The dynamic order in which procedure is applied to the
elements of vector is unspecified.
(vector-map cadr '#((a b) (d e) (g h))) => #(b e h) (vector-map (lambda (n) (expt n n)) '#(1 2 3 4)) => #(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 none. The
search operation takes time proportional to the logarithm of the length
of vector. Unwrap-key must be a procedure that maps each
element of vector to a key. Key<? must be a procedure that
implements a total ordering on the keys of the elements.
(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!
sort!
returns vector as its value.
Two sorting algorithms are implemented: merge-sort!
and
quick-sort!
. The procedure sort!
is an alias for
merge-sort!
.
See also the definition of sort
.
Go to the first, previous, next, last section, table of contents.