Vectors

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 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 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 follows:

#(0 (2 2 2 2) "Anna")

Vector constants are self-evaluating, so they do not need to be quoted in programs.

(vector? obj ) procedure
Returns #t if obj is a vector; otherwise returns #f.

(make-vector k ) procedure
(make-vector 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)

(vector-length vector ) procedure
Returns the number of elements in vector as an exact integer.

(vector-ref vector k ) procedure
It is an error if k is not a valid index of vector.

The vector-ref procedure 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) (exact (round (* 2 (acos -1))))) ==> 13

(vector-set! vector k obj ) special form
It is an error if k is not a valid index of vector.

The vector-set! procedure stores obj in element k of vector.
(let ((vec (vector 0 '(2 2 2 2) "Anna"))) (vector-set! 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"

(vector-copy vector ) procedure
(vector-copy vector start ) procedure
(vector-copy 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 (vector-copy a)) (vector-set! b 0 3) b ==> #(3 8 2 8) (define c (vector-copy b 1 3)) c ==> #(8 2)

(vector-copy! to at from ) procedure
(vector-copy! to at from start ) procedure
(vector-copy! 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 (- (vector-length 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)) (vector-copy! b 1 a 0 2) b ==> #(10 1 2 40 50)

(vector-append vector ... ) procedure
Returns a newly allocated vector whose elements are the concatenation of the elements of the given vectors.
(vector-append #(a b c) #(d e f)) ==> #(a b c d e f)

(vector-fill! vector fill ) procedure
(vector-fill! vector fill start ) procedure
(vector-fill! vector fill start end ) procedure
The vector-fill! procedure stores fill in the elements of vector between start and end.
(define a (vector 1 2 3 4 5)) (vector-fill! a 'smash 2 4) a ==> #(1 2 smash smash 5)

husk-scheme online documentation rev 3.2 (2021.03.04)