Next: , Previous: , Up: Vectors   [Contents][Index]


8.1 Construction of Vectors

procedure: make-vector k [object]

Returns a newly allocated vector of k elements. If object is specified, make-vector initializes each element of the vector to object. Otherwise the initial elements of the result are unspecified.

procedure: vector object …

Returns a newly allocated vector whose elements are the given arguments. vector is analogous to list.

(vector 'a 'b 'c)                       ⇒  #(a b c)
procedure: vector-copy vector

Returns a newly allocated vector that is a copy of vector.

procedure: list->vector list

Returns a newly allocated vector initialized to the elements of list. The inverse of list->vector is vector->list.

(list->vector '(dididit dah))           ⇒  #(dididit dah)
standard procedure: string->vector string [start [end]]
standard procedure: vector->string vector [start [end]]

It is an error if any element of vector 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"
procedure: make-initialized-vector k initialization

Similar to make-vector, except that the elements of the result are determined by calling the procedure initialization on the indices. For example:

(make-initialized-vector 5 (lambda (x) (* x x)))
     ⇒  #(0 1 4 9 16)
procedure: vector-grow vector k

K must be greater than or equal to the length of vector. Returns a newly allocated vector of length k. The first (vector-length vector) elements of the result are initialized from the corresponding elements of vector. The remaining elements of the result are unspecified.

standard procedure: vector-map procedure vector vector …

It is an error if procedure does not accept as many arguments as there are vectors and return a single value.

The vector-map procedure applies procedure element-wise to the elements of the vectors and returns a vector of the results, in order. If more than one vector is given and not all vectors are the same length, vector-map terminates when the shortest vector runs out. The dynamic order in which procedure is applied to the elements of the vectors is unspecified. If multiple returns occur from vector-map, the values returned by earlier returns are not mutated.

(vector-map cadr '#((a b) (d e) (g h))) ⇒  #(b e h)
(vector-map (lambda (n) (expt n n)) '#(1 2 3 4 5))
                                        ⇒  #(1 4 27 256 3125)
(vector-map + '#(1 2 3) '#(4 5 6 7))    ⇒  #(5 7 9)
(let ((count 0))
  (vector-map (lambda (ignored)
                (set! count (+ count 1))
                count)
              '#(a b)))                 ⇒ #(1 2) or #(2 1)
standard procedure: vector-for-each procedure vector vector …

It is an error if procedure does not accept as many arguments as there are vectors.

The arguments to vector-for-each are like the arguments to vector-map, but vector-for-each calls procedure for its side effects rather than for its values. Unlike vector-map, vector-for-each is guaranteed to call procedure on the elements of the vectors in order from the first element(s) to the last, and the value returned by vector-for-each is unspecified. If more than one vector is given and not all vectors have the same length, vector-for-each terminates when the shortest vector runs out. It is an error for procedure to mutate any of the vectors.


Next: Selecting Vector Components, Previous: Vectors, Up: Vectors   [Contents][Index]