Previous: Strings, Up: Other data types [Contents][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 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 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 following:
#(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")
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. Analogous to ‘list’.
(vector 'a 'b 'c) ==> #(a b c)
Returns the number of elements in vector as an exact integer.
k must be a valid index of vector. ‘Vector-ref’ 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) (let ((i (round (* 2 (acos -1))))) (if (inexact? i) (inexact->exact i) i))) ==> 13
k must be a valid index of vector. ‘Vector-set!’ stores obj in element k of vector. The value returned by ‘vector-set!’ is unspecified.
(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
‘Vector->list’ returns a newly allocated list of the objects contained in the elements of vector. ‘List->vector’ returns a newly created vector initialized to the elements of the list list.
(vector->list '#(dah dah didah)) ==> (dah dah didah) (list->vector '(dididit dah)) ==> #(dididit dah)
Stores fill in every element of vector. The value returned by ‘vector-fill!’ is unspecified.
Previous: Strings, Up: Other data types [Contents][Index]