Previous: Strings, Up: Other data types



6.3.6 Vectors

Vectors are heterogenous 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")
     
— procedure: vector? obj

Returns #t if obj is a vector, otherwise returns #f.

— procedure: make-vector k
— procedure: make-vector k fill

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.

— library procedure: vector obj ...

Returns a newly allocated vector whose elements contain the given arguments. Analogous to list.

(vector 'a 'b 'c)                      ==>  #(a b c)
     
— procedure: vector-length vector

Returns the number of elements in vector as an exact integer.

— procedure: vector-ref vector k

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
     
— procedure: vector-set! vector k obj

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
     
— library procedure: vector->list vector
— library procedure: list->vector list

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)
     
— library procedure: vector-fill! vector fill

Stores fill in every element of vector. The value returned by vector-fill! is unspecified.