6.6.10.3 Accessing and Modifying Vector Contents

vector-length and vector-ref return information about a given vector, respectively its size and the elements that are contained in the vector.

Scheme Procedure: vector-length vector
C Function: scm_vector_length (vector)

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

C Function: size_t scm_c_vector_length (SCM vec)

Return the number of elements in vec as a size_t.

Scheme Procedure: vector-ref vec k
C Function: scm_vector_ref (vec, k)

Return the contents of position k of vec. k must be a valid index of vec.

(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
C Function: SCM scm_c_vector_ref (SCM vec, size_t k)

Return the contents of position k (a size_t) of vec.

A vector created by one of the dynamic vector constructor procedures (see Dynamic Vector Creation and Validation) can be modified using the following procedures.

NOTE: According to R5RS, it is an error to use any of these procedures on a literally read vector, because such vectors should be considered as constants. Currently, however, Guile does not detect this error.

Scheme Procedure: vector-set! vec k obj
C Function: scm_vector_set_x (vec, k, obj)

Store obj in position k of vec. k must be a valid index of vec. 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")
C Function: void scm_c_vector_set_x (SCM vec, size_t k, SCM obj)

Store obj in position k (a size_t) of vec.

Scheme Procedure: vector-fill! vec fill [start [end]]
C Function: scm_vector_fill_x (vec, fill)

Store fill in every position of vec in the range [start ... end). start defaults to 0 and end defaults to the length of vec.

The value returned by vector-fill! is unspecified.

Scheme Procedure: vector-copy vec [start [end]]
C Function: scm_vector_copy (vec)

Returns a freshly allocated vector containing the elements of vec in the range [start ... end). start defaults to 0 and end defaults to the length of vec.

Scheme Procedure: vector-copy! dst at src [start [end]]

Copy the block of elements from vector src in the range [start ... end) into vector dst, starting at position at. at and start default to 0 and end defaults to the length of src.

It is an error for dst to have a length less than at + (end - start).

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.

The value returned by vector-copy! is unspecified.

Scheme Procedure: vector-move-left! vec1 start1 end1 vec2 start2
C Function: scm_vector_move_left_x (vec1, start1, end1, vec2, start2)

Copy elements from vec1, positions start1 to end1, to vec2 starting at position start2. start1 and start2 are inclusive indices; end1 is exclusive.

vector-move-left! copies elements in leftmost order. Therefore, in the case where vec1 and vec2 refer to the same vector, vector-move-left! is usually appropriate when start1 is greater than start2.

The value returned by vector-move-left! is unspecified.

Scheme Procedure: vector-move-right! vec1 start1 end1 vec2 start2
C Function: scm_vector_move_right_x (vec1, start1, end1, vec2, start2)

Copy elements from vec1, positions start1 to end1, to vec2 starting at position start2. start1 and start2 are inclusive indices; end1 is exclusive.

vector-move-right! copies elements in rightmost order. Therefore, in the case where vec1 and vec2 refer to the same vector, vector-move-right! is usually appropriate when start1 is less than start2.

The value returned by vector-move-right! is unspecified.