[ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

## 6.14 Vectors

Builtin Class: <vector>

A vector is a simple 1-dimensional array of Scheme objects. You can access its element by index in constant time. Once created, a vector can’t be resized.

Class `<vector>` inherits `<sequence>` and you can use various generic functions such as `map` and `fold` on it. See section `gauche.collection` - Collection framework, and See section `gauche.sequence` - Sequence framework.

If you keep only a homogeneous numeric type, you may be able to use SRFI-4 homogeneous vectors (See section `srfi-4` - Homogeneous vectors).

R7RS defines bytevectors; in Gauche, they’re just `u8vector`s in `gauche.uvector` module (r7rs modules defines aliases. See section `scheme.base` - R7RS base library).

See section `srfi-43` - Vector library, for additional operations on vectors.

Function: vector? obj

[R7RS] Returns `#t` if obj is a vector, `#f` otherwise.

Function: make-vector k :optional fill

[R7RS] Creates and returns a vector with length k. If optional argument fill is given, each element of the vector is initialized by it. Otherwise, the initial value of each element is undefined.

Function: vector obj …

[R7RS] Creates a vector whose elements are obj ….

Function: vector-tabulate len proc

Creates a vector of length len, initializing i-th element of which by `(proc i)` for all i between `0` and len

 ```(vector-tabulate 5 (^x (* x x))) ⇒ #(0 1 4 9 16) ```
Function: vector-length vector

[R7RS] Returns the length of a vector vector.

With `gauche.collection` module, you can also use a method `size-of`.

Function: vector-ref vector k :optional fallback

[R7RS+] Returns k-th element of vector vector.

By default, `vector-ref` signals an error if k is negative, or greater than or equal to the length of vector. However, if an optional argument fallback is given, it is returned for such case. This is an extension of Gauche.

With `gauche.sequence` module, you can also use a method `ref`.

Function: vector-set! vector k obj

[R7RS] Sets k-th element of the vector vector to obj. It is an error if k is negative or greater than or equal to the length of vector.

With `gauche.sequence` module, you can also use a setter method of `ref`.

Function: vector->list vector :optional start end
Function: list->vector list :optional start end

[R7RS+][SRFI-43+] Converts a vector to a list, or vice versa.

The optional start and end arguments limit the range of the source. (R7RS doesn’t define start and end arguments for `list->vector`.)

 ```(vector->list '#(1 2 3 4 5)) ⇒ (1 2 3 4 5) (list->vector '(1 2 3 4 5)) ⇒ #(1 2 3 4 5) (vector->list '#(1 2 3 4 5) 2 4) ⇒ (3 4) (list->vector (circular-list 'a 'b 'c) 1 6) ⇒ #(b c a b c) ```

With gauche.collection module, you can use `(coerce-to <list> vector)` and `(coerce-to <vector> list)` as well.

Function: vector->string vector :optional start end
Function: string->vector string :optional start end

[R7RS] Converts a vector of characters to a string, or vice versa. It is an error to pass a vector that contains other than characters to `vector->string`.

The optional start and end arguments limit the range of the source.

 ```(vector->list '#(1 2 3 4 5)) ⇒ (1 2 3 4 5) (list->vector '(1 2 3 4 5)) ⇒ #(1 2 3 4 5) (vector->list '#(1 2 3 4 5) 2 4) ⇒ (3 4) (list->vector (circular-list 'a 'b 'c) 1 6) ⇒ #(b c a b c) ```

With gauche.collection module, you can use `(coerce-to <string> vector)` and `(coerce-to <vector> string)` as well.

Function: vector-fill! vector fill :optional start end

[R7RS][SRFI-43] Sets all elements in a vector vector to fill.

Optional start and end limits the range of effect between start-th index (inclusive) to end-th index (exclusive). Start defaults to zero, and end defaults to the length of vector.

Function: vector-copy vector :optional start end fill

[R7RS][SRFI-43] Copies a vector vector. Optional start and end arguments can be used to limit the range of vector to be copied. If the range specified by start and end falls outside of the original vector, the fill value is used to fill the result vector.

 ```(vector-copy '#(1 2 3 4 5)) ⇒ #(1 2 3 4 5) (vector-copy '#(1 2 3 4 5) 2 4) ⇒ #(3 4) (vector-copy '#(1 2 3 4 5) 3 7 #f) ⇒ #(4 5 #f #f) ```
Function: vector-map proc vec1 vec2 …

[R7RS] Returns a new vector, i-th of which is calculated by applying proc on the list of each i-th element of vec1 vec2 …. The length of the result vector is the same as the shortest vector of the arguments.

 ```(vector-map + '#(1 2 3) '#(4 5 6 7)) ⇒ #(5 7 9) ```

The actual order proc is called is undefined, and may change in the future versions, so proc shouldn’t use side effects affected by the order.

Note: If you use `gauche.collection`, you can get the same function by `(map-to <vector> proc vec1 vec2 …`.

Note: SRFI-43 also provides `vector-map`, but the API is slightly different (See section `srfi-43` - Vector library). SRFI-43’s `vector-map` is equivalent to `vector-map-with-index` below.

Function: vector-map-with-index proc vec1 vec2 …

Like `vector-map`, but proc receives the current index as the first argument.

 ```(vector-map-with-index list '#(a b c d e) '#(A B C)) ⇒ #((0 a A) (1 b B) (2 c C)) ```

This is what SRFI-43 calls `vector-map`.

Note: If you use `gauche.collection`, you can get the same function by `(map-to-with-index <vector> proc vec1 vec2 …`.

Function: vector-map! proc vec1 vec2 …

For each index i, calls proc with i-th index of vec1 vec2 …, and set the result back to vec1. The value is calculated up to the minimum length of input vectors.

 ```(rlet1 v (vector 1 2 3) (vector-map! (\$ + 1 \$) v)) ⇒ #(2 3 4) (rlet1 v (vector 1 2 3 4) (vector-map! + v '#(10 20))) ⇒ #(11 22 3 4) ```

Note: SRFI-43 provides `vector-map!` with slightly different API (See section `srfi-43` - Vector library). It is equivalent to `vector-map-with-index!` below.

Function: vector-map-with-index! proc vec1 vec2 …

Like `vector-map!`, but proc receives the current index as the first argument. This is equivalent to SRFI-43’s `vector-map!` (See section `srfi-43` - Vector library).

 ```(rlet1 v (vector 'a 'b 'c) (vector-map-with-index! list v)) ⇒ #((0 a) (1 b) (2 c)) ```
Function: vector-for-each proc vec1 vec2 …

[R7RS] For all i below the minimum length of input vectors, calls proc with i-th elements of vec1 vec2 …, in increasing order of i.

 ```(vector-for-each print '#(a b c)) ⇒ prints `a`, `b` and `c`. ```

Note: SRFI-43 provides `vector-for-each` with slightly different API (See section `srfi-43` - Vector library). It is equivalent to `vector-for-each-with-index` described below.

Function: vector-for-each-with-index proc vec1 vec2 …

Like `vector-for-each`, but proc receives the current index in the first argument.

This is equivalent to SRFI-43’s `vector-for-each`.

 [ < ] [ > ] [ << ] [ Up ] [ >> ]

This document was generated on July 19, 2014 using texi2html 1.82.