Next: , Previous: Construction of Vectors, Up: Vectors

8.2 Selecting Vector Components

— procedure: vector? object

Returns #t if object is a vector; otherwise returns #f.

— procedure: vector-length vector

Returns the number of elements in vector.

— procedure: vector-ref vector k

Returns the contents of element k of vector. K must be a valid index of vector.

          (vector-ref '#(1 1 2 3 5 8 13 21) 5)    =>  8
— procedure: vector-set! vector k object

Stores object in element k of vector and returns an unspecified value. K must be a valid index of vector.

          (let ((vec (vector 0 '(2 2 2 2) "Anna")))
            (vector-set! vec 1 '("Sue" "Sue"))
               =>  #(0 ("Sue" "Sue") "Anna")
— procedure: vector-first vector
— procedure: vector-second vector
— procedure: vector-third vector
— procedure: vector-fourth vector
— procedure: vector-fifth vector
— procedure: vector-sixth vector
— procedure: vector-seventh vector
— procedure: vector-eighth vector

These procedures access the first several elements of vector in the obvious way. It is an error if the implicit index of one of these procedurs is not a valid index of vector.

— procedure: vector-binary-search vector key<? unwrap-key key

Searches vector for an element with a key matching key, returning the element if one is found or #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