For Gauche 0.9.5


Next: , Previous: , Up: Core library   [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 Collection framework, and See Sequence framework.

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

R7RS defines bytevectors; in Gauche, they’re just u8vectors in gauche.uvector module (r7rs modules defines aliases. see R7RS base library).

See 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-133+] Converts a vector to a list, or vice versa.

The optional start and end arguments limit the range of the source. (R7RS and SRFI-133 don’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: reverse-list->vector list :optional start end

[SRFI-133] Without optional arguments, it returns the same thing as (list->vector (reverse list)), but does not allocate the intermediate list. The optional start and end argument limits the range of the input list.

(reverse-list->vector '(a b c d e f g) 1 5)
  ⇒ #(e d c b)
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->string '#(#\a #\b #\c #\d #\e))     ⇒ "abcde"
(string->vector "abcde")                     ⇒ #(#\a #\b #\c #\d #\e)
(vector->string '#(#\a #\b #\c #\d #\e) 2 4) ⇒ ("cd")

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-133] 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-133] 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-copy! target tstart source :optional sstart send

[R7RS][SRFI-133] Copies the content of source vector into the target vector starting from tstart in the target. The target vector must be mutable. Optional sstart and send limits the range of source vector.

(rlet1 v (vector 'a 'b 'c 'd 'e)
  (vector-copy! v 2 '#(1 2)))
  ⇒ #(a b 1 2 e)
(rlet1 v (vector 'a 'b 'c 'd 'e)
  (vector-copy! v 2 '#(1 2 3 4) 1 3))
  ⇒ #(a b 2 3 e)

An error is raised if the portion to be copied is greater than the room in the target (that is, between tstart to the end).

It is ok to pass the same vector to target and source; it always works even if the regions of source and destination are overlapping.

Function: vector-append vec …

[R7RS][SRFI-133] Returns a newly allocated vector whose contents are concatenation of elements of vec in order.

(vector-append '#(1 2 3) '#(a b)) ⇒ #(1 2 3 a b)
(vector-append) ⇒ #()
Function: vector-map proc vec1 vec2 …

[R7RS][SRFI-133] 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 ….

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. See Vector library (Legacy).

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 …

[SRFI-133] 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)
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 Vector library (Legacy)).

(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][SRFI-133] 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.
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. See Vector library (Legacy).


Next: , Previous: , Up: Core library   [Contents][Index]