VectorsVectors 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 nonnegative integer that is fixed when the vector is created. The valid indexes of a vector are the exact nonnegative 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 selfevaluating, so they do not need to be quoted in programs.
(vector?
obj
)
procedure
Returns #t if obj is a vector; otherwise returns #f.
(makevector
k
)
procedure
(makevector
k fill
)
procedure
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 unspeci
fied.
(vector
obj ...
)
procedure
Returns a newly allocated vector whose elements contain
the given arguments. It is analogous to list.
(vector 'a 'b 'c) ==> #(a b c)
(vectorlength
vector
)
procedure
Returns the number of elements in vector as an exact integer.
(vectorref
vector k
)
procedure
It is an error if k is not a valid index of vector.
The vectorref procedure returns the contents of element k of vector.
(vectorref '#(1 1 2 3 5 8 13 21)
5)
==> 8
(vectorref '#(1 1 2 3 5 8 13 21)
(exact
(round (* 2 (acos 1)))))
==> 13
(vectorset!
vector k obj
)
special form
It is an error if k is not a valid index of vector.
The vectorset! procedure stores obj in element k of vector.
(let ((vec (vector 0 '(2 2 2 2) "Anna")))
(vectorset! vec 1 '("Sue" "Sue"))
vec)
==> #(0 ("Sue" "Sue") "Anna")
(vector>list
vector
)
procedure
(vector>list
vector start
)
procedure
(vector>list
vector start end
)
procedure
(list>vector
list
)
procedure
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)
(vector>string
vector
)
procedure
(vector>string
vector start
)
procedure
(vector>string
vector start end
)
procedure
(string>vector
vector
)
procedure
(string>vector
vector start
)
procedure
(string>vector
vector start end
)
procedure
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"
(vectorcopy
vector
)
procedure
(vectorcopy
vector start
)
procedure
(vectorcopy
vector start end
)
procedure
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))
(define b (vectorcopy a))
(vectorset! b 0 3)
b ==> #(3 8 2 8)
(define c (vectorcopy b 1 3))
c ==> #(8 2)
(vectorcopy!
to at from
)
procedure
(vectorcopy!
to at from start
)
procedure
(vectorcopy!
to at from start end
)
procedure
It is an error if at is less than zero or greater than the length
of to. It is also an error if ( (vectorlength 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))
(vectorcopy! b 1 a 0 2)
b ==> #(10 1 2 40 50)
(vectorappend
vector ...
)
procedure
Returns a newly allocated vector whose elements are the
concatenation of the elements of the given vectors.
(vectorappend #(a b c) #(d e f))
==> #(a b c d e f)
(vectorfill!
vector fill
)
procedure
(vectorfill!
vector fill start
)
procedure
(vectorfill!
vector fill start end
)
procedure
The vectorfill! procedure stores fill in the elements of
vector between start and end.
(define a (vector 1 2 3 4 5))
(vectorfill! a 'smash 2 4)
a
==> #(1 2 smash smash 5)
