Next: Bytevectors, Previous: Strings, Up: Standard procedures [Index]
Vectors are heterogeneous structures whose elements are indexed by integers. A vector typically occupies less space than a list of the same length, and the average time needed 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 a 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 #(
obj …)
.
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 follows:
#(0 (2 2 2 2) "Anna")
Vector constants are self-evaluating, so they do not need to be quoted in programs.
Returns #t
if obj is a vector; otherwise returns
#f
.
Returns a newly allocated vector of k elements. If a second argument is given, then each element is initialized to fill. Otherwise the initial contents of each element is unspecified.
Returns a newly allocated vector whose elements contain the given
arguments. It is analogous to list
.
(vector 'a 'b 'c) ⇒ #(a b c)
Returns the number of elements in vector as an exact integer.
It is an error if k is not a valid index of vector.
The vector-ref
procedure returns the contents of element k
of vector.
(vector-ref '#(1 1 2 3 5 8 13 21) 5) ⇒ 8 (vector-ref '#(1 1 2 3 5 8 13 21) (exact (round (* 2 (acos -1))))) ⇒ 13
It is an error if k is not a valid index of vector.
The vector-set!
procedure stores obj in element k of
vector.
(let ((vec (vector 0 '(2 2 2 2) "Anna")))
(vector-set! vec 1 '("Sue" "Sue"))
vec)
⇒ #(0 ("Sue" "Sue") "Anna")
(vector-set! '#(0 1 2) 1 "doe") ⇒ error ; constant vector
The vector->list
procedure returns a newly allocated list of the
objects contained in the elements of vector between start
and end. The list->vector
procedure returns a newly
created vector initialized to the elements of the list list.
In both procedures, order is preserved.
(vector->list '#(dah dah didah)) ⇒ (dah dah didah) (vector->list '#(dah dah didah) 1 2) ⇒ (dah) (list->vector '(dididit dah)) ⇒ #(dididit dah)
It is an error if any element of vector between start and end is not a character.
The vector->string
procedure returns a newly allocated string of
the objects contained in the elements of vector between
start and end. The string->vector
procedure returns
a newly created vector initialized to the elements of the string
string between start and end.
In both procedures, order is preserved.
(string->vector "ABC") ⇒ #(#\A #\B #\C) (vector->string #(#\1 #\2 #\3) ⇒ "123"
Returns a newly allocated copy of the elements of the given
vector between start and end. The elements of the new
vector are the same (in the sense of eqv?
) as the elements of
the old.
(define a #(1 8 2 8)) ; a may be immutable (define b (vector-copy a)) (vector-set! b 0 3) ; b is mutable b ⇒ #(3 8 2 8) (define c (vector-copy b 1 3)) c ⇒ #(8 2)
It is an error if at is less than zero or greater than the
length of to. It is also an error if
(- (vector-length
to)
at)
is less than (-
end start)
.
Copies the elements of vector from between start and end to vector to, starting at at. The order in which elements are copied is unspecified, except that if the source and destination overlap, copying takes place as if the source is first copied into a temporary vector and then into the destination. This can be achieved without allocating storage by making sure to copy in the correct direction in such circumstances.
(define a (vector 1 2 3 4 5)) (define b (vector 10 20 30 40 50)) (vector-copy! b 1 a 0 2) b ⇒ #(10 1 2 40 50)
Returns a newly allocated vector whose elements are the concatenation of the elements of the given vectors.
(vector-append #(a b c) #(d e f)) ⇒ #(a b c d e f)
The vector-fill!
procedure stores fill in the elements of
vector between start and end.
(define a (vector 1 2 3 4 5)) (vector-fill! a 'smash 2 4) a ⇒ #(1 2 smash smash 5)
Next: Bytevectors, Previous: Strings, Up: Standard procedures [Index]