4.12 Vectors
On this page:
top
up

4.12VectorsπŸ”— i

+Vectors in The Racket Guide introduces vectors.

A vector is a fixed-length array with constant-time access and update of the vector slots, which are numbered from 0 to one less than the number of slots in the vector.

Two vectors are equal? if they have the same length, and if the values in corresponding slots of the vectors are equal? .

A vector can be mutable or immutable. When an immutable vector is provided to a procedure like vector-set! , the exn:fail:contract exception is raised. Vectors generated by the default reader (see Reading Strings) are immutable. Use immutable? to check whether a vector is immutable.

A vector can be used as a single-valued sequence (see Sequences). The elements of the vector serve as elements of the sequence. See also in-vector .

A literal or printed vector starts with #(, optionally with a number between the # and (. See Reading Vectors for information on read ing vectors and Printing Vectors for information on print ing vectors.

procedure

(vector? v)boolean?

v:any/c
Returns #t if v is a vector, #f otherwise.

See also immutable-vector? and mutable-vector? .

procedure

(make-vector size[v])vector?

v:any/c =0
Returns a mutable vector with size slots, where all slots are initialized to contain v. Note that v is shared for all elements, so for mutable data, mutating an element will affect other elements.

Examples:
> (make-vector 32)

'#(2 2 2)

> (define v(make-vector 5(box 3)))
> v

'#(#&3 #&3 #&3 #&3 #&3)

> (set-box! (vector-ref v0)7)
> v

'#(#&7 #&7 #&7 #&7 #&7)

This function takes time proportional to size.

procedure

(vector v...)vector?

v:any/c
Returns a newly allocated mutable vector with as many slots as provided vs, where the slots are initialized to contain the given vs in order.

procedure

v:any/c
Returns a newly allocated immutable vector with as many slots as provided vs, where the slots contain the given vs in order.

vec:vector?
Returns the length of vec (i.e., the number of slots in the vector).

This function takes constant time.

procedure

(vector-ref vecpos)any/c

vec:vector?
Returns the element in slot pos of vec. The first slot is position 0, and the last slot is one less than (vector-length vec).

This function takes constant time.

procedure

(vector-set! vecposv)void?

v:any/c
Updates the slot pos of vec to contain v.

This function takes constant time.

Like vector-length , vector-ref , and vector-set! , but constrained to work on vectors that are not impersonators.

Added in version 6.90.0.15 of package base.

procedure

(vector-cas! vecposold-vnew-v)boolean?

old-v:any/c
new-v:any/c
Compare and set operation for vectors. See box-cas! .

Added in version 6.11.0.2 of package base.

procedure

(vector->list vec)list?

vec:vector?
Returns a list with the same length and elements as vec.

This function takes time proportional to the size of vec.

procedure

(list->vector lst)vector?

lst:list?
Returns a mutable vector with the same length and elements as lst.

This function takes time proportional to the length of lst.

vec:vector?
Returns an immutable vector with the same length and elements as vec. If vec is itself immutable, then it is returned as the result.

This function takes time proportional to the size of vec when vec is mutable.

procedure

(vector-fill! vecv)void?

v:any/c
Changes all slots of vec to contain v.

This function takes time proportional to the size of vec.

procedure

(vector-copy! dest
dest-start
src
[ src-start
src-end])void?
src:vector?
Changes the elements of dest starting at position dest-start to match the elements in src from src-start (inclusive) to src-end (exclusive). The vectors dest and src can be the same vector, and in that case the destination region can overlap with the source region; the destination elements after the copy match the source elements from before the copy. If any of dest-start, src-start, or src-end are out of range (taking into account the sizes of the vectors and the source and destination regions), the exn:fail:contract exception is raised.

This function takes time proportional to (- src-endsrc-start).

Examples:
> (define v(vector 'A'p'p'l'e))
> (vector-copy! v4#(y))
> (vector-copy! v0v34)
> v

'#(l p p l y)

procedure

(vector->values vec[start-posend-pos])any

vec:vector?
Returns end-pos - start-pos values, which are the elements of vec from start-pos (inclusive) to end-pos (exclusive). If start-pos or end-pos are greater than (vector-length vec), or if end-pos is less than start-pos, the exn:fail:contract exception is raised.

This function takes time proportional to the size of vec.

procedure

( build-vector nproc)vector?

Creates a vector of n elements by applying proc to the integers from 0 to (sub1 n) in order. If vec is the resulting vector, then (vector-ref veci) is the value produced by (proci).

Example:

'#(1 2 3 4 5)

4.12.1Additional Vector FunctionsπŸ”— i

(require racket/vector ) package: base
The bindings documented in this section are provided by the racket/vector and racket libraries, but not racket/base.

procedure

( vector-empty? v)boolean?

Returns #t if v is empty (i.e. its length is 0), #f otherwise.

Added in version 7.4.0.4 of package base.

procedure

( vector-set*! vecposv......)void?

v:any/c
Updates each slot pos of vec to contain each v. The update takes place from the left so later updates overwrite earlier updates.

procedure

( vector-map procvec...+)vector?

proc:procedure?
vec:vector?
Applies proc to the elements of the vecs from the first elements to the last. The proc argument must accept the same number of arguments as the number of supplied vecs, and all vecs must have the same number of elements. The result is a fresh vector containing each result of proc in order.

Example:
> (vector-map + #(12)#(34))

'#(4 6)

procedure

( vector-map! procvec...+)vector?

proc:procedure?
Like vector-map , but result of proc is inserted into the first vec at the index that the arguments to proc were taken from. The result is the first vec.

Examples:
> (define v(vector 1234))

'#(2 3 4 5)

> v

'#(2 3 4 5)

procedure

( vector-append vec...)vector?

vec:vector?
Creates a fresh vector that contains all of the elements of the given vectors in order.

Example:
> (vector-append #(12)#(34))

'#(1 2 3 4)

procedure

( vector-take vecpos)vector?

vec:vector?
Returns a fresh vector whose elements are the first pos elements of vec. If vec has fewer than pos elements, then the exn:fail:contract exception is raised.

Example:
> (vector-take #(1234)2)

'#(1 2)

Returns a fresh vector whose elements are the last pos elements of vec. If vec has fewer than pos elements, then the exn:fail:contract exception is raised.

Example:
> (vector-take-right #(1234)2)

'#(3 4)

procedure

( vector-drop vecpos)vector?

vec:vector?
Returns a fresh vector whose elements are the elements of vec after the first pos elements. If vec has fewer than pos elements, then the exn:fail:contract exception is raised.

Example:
> (vector-drop #(1234)2)

'#(3 4)

Returns a fresh vector whose elements are the prefix of vec, dropping its pos-length tail. If vec has fewer than pos elements, then the exn:fail:contract exception is raised.

Examples:
> (vector-drop-right #(1234)1)

'#(1 2 3)

> (vector-drop-right #(1234)3)

'#(1)

Returns the same result as

(values (vector-take vecpos)(vector-drop vecpos))

except that it can be faster.

Example:
> (vector-split-at #(12345)2)

'#(1 2)

'#(3 4 5)

Returns the same result as

(values (vector-take-right vecpos)(vector-drop-right vecpos))

except that it can be faster.

Example:
> (vector-split-at-right #(12345)2)

'#(1 2 3)

'#(4 5)

Creates a fresh vector of size (- endstart), with all of the elements of vec from start (inclusive) to end (exclusive).

Examples:
> (vector-copy #(1234))

'#(1 2 3 4)

> (vector-copy #(1234)3)

'#(4)

> (vector-copy #(1234)23)

'#(3)

procedure

( vector-set/copy vecposval)vector?

vec:vector?
val:any/c
Creates a fresh vector with the same content as vec, except that val is the element at index pos.

Examples:
> (vector-set/copy #(123)0'x)

'#(x 2 3)

> (vector-set/copy #(123)2'x)

'#(1 2 x)

Added in version 8.11.1.10 of package base.

procedure

( vector-extend vecnew-size[val])vector?

vec:vector?
val:any/c =0
Creates a fresh vector of length new-size where the prefix is filled with the elements of vec and the remainder with val.

Examples:
> (vector-extend #(123)10)

'#(1 2 3 0 0 0 0 0 0 0)

> (vector-extend #(123)10#f)

'#(1 2 3 #f #f #f #f #f #f #f)

> (vector-extend #(123)3#f)

'#(1 2 3)

Added in version 8.12.0.10 of package base.

procedure

( vector-filter predvec)vector?

pred:procedure?
vec:vector?
Returns a fresh vector with the elements of vec for which pred produces a true value. The pred procedure is applied to each element from first to last.

Example:
> (vector-filter even? #(123456))

'#(2 4 6)

procedure

( vector-filter-not predvec)vector?

pred:procedure?
vec:vector?
Like vector-filter , but the meaning of the pred predicate is reversed: the result is a vector of all items for which pred returns #f.

Example:
> (vector-filter-not even? #(123456))

'#(1 3 5)

procedure

( vector-count procvec...+)exact-nonnegative-integer?

proc:procedure?
vec:vector?
Returns the number of elements of the vec... (taken in parallel) on which proc does not evaluate to #f.

Examples:
> (vector-count even? #(12345))

2

> (vector-count = #(12345)#(54321))

1

procedure

( vector-argmin procvec)any/c

proc:(-> any/c real? )
vec:vector?
This returns the first element in the non-empty vector vec that minimizes the result of proc.

Examples:
> (vector-argmin car #((3pears)(1banana)(2apples)))

'(1 banana)

> (vector-argmin car #((1banana)(1orange)))

'(1 banana)

procedure

( vector-argmax procvec)any/c

proc:(-> any/c real? )
vec:vector?
This returns the first element in the non-empty vector vec that maximizes the result of proc.

Examples:
> (vector-argmax car #((3pears)(1banana)(2apples)))

'(3 pears)

> (vector-argmax car #((3pears)(3oranges)))

'(3 pears)

procedure

( vector-member vvec[is-equal?])(or/c natural-number/c #f)

v:any/c
vec:vector?
is-equal?:(-> any/c any/c any/c )=equal?
Locates the first element of vec that is equal to v according to is-equal?. If such an element exists, the index of that element in vec is returned. Otherwise, the result is #f.

Examples:
> (vector-member 2(vector 1234))

1

> (vector-member 9(vector 1234))

#f

> (vector-member 1.0(vector 1234)= )

0

Changed in version 8.15.0.1 of package base: Added the is-equal? argument.

procedure

( vector-memv vvec)(or/c natural-number/c #f)

v:any/c
vec:vector?
Like vector-member , but finds an element using eqv? .

Examples:
> (vector-memv 2(vector 1234))

1

> (vector-memv 9(vector 1234))

#f

procedure

( vector-memq vvec)(or/c natural-number/c #f)

v:any/c
vec:vector?
Like vector-member , but finds an element using eq? .

Examples:
> (vector-memq 2(vector 1234))

1

> (vector-memq 9(vector 1234))

#f

procedure

( vector-sort vec
less-than?
[ start
end
#:keykey
#:cache-keys?cache-keys?])vector?
vec:vector?
less-than?:(any/c any/c . -> .any/c )
key:(or/c #f(any/c . -> .any/c ))=#f
cache-keys?:boolean? =#f
Like sort , but operates on vectors; a fresh vector of length (- endstart) is returned containing the elements from indices start (inclusive) through end (exclusive) of vec, but in sorted order (i.e., vec is not modified). This sort is stable (i.e., the order of “equal” elements is preserved).

If end is #f, it is replaced with (vector-length vec).

Examples:
> (define v1(vector 4321))
> v1

'#(4 3 2 1)

> (vector-sort v1< )

'#(1 2 3 4)

> v1

'#(4 3 2 1)

> (vector-sort v1< 2#f#:key#f)

'#(1 2)

> v1

'#(4 3 2 1)

> (define v2(vector '(4)'(3)'(2)'(1)))
> v2

'#((4) (3) (2) (1))

> (vector-sort v2< 13#:keycar )

'#((2) (3))

> v2

'#((4) (3) (2) (1))

Added in version 6.6.0.5 of package base.

procedure

( vector-sort! vec
less-than?
[ start
end
#:keykey
#:cache-keys?cache-keys?])void?
less-than?:(any/c any/c . -> .any/c )
key:(or/c #f(any/c . -> .any/c ))=#f
cache-keys?:boolean? =#f
Like vector-sort , but updates indices start (inclusive) through end (exclusive) of vec by sorting them according to the less-than? procedure.

Examples:
> (define v1(vector 4321))
> v1

'#(4 3 2 1)

> (vector-sort! v1< )
> v1

'#(1 2 3 4)

> (define v2(vector 4321))
> v2

'#(4 3 2 1)

> (vector-sort! v2< 2#f#:key#f)
> v2

'#(4 3 1 2)

> (define v3(vector '(4)'(3)'(2)'(1)))
> v3

'#((4) (3) (2) (1))

> (vector-sort! v3< 13#:keycar )
> v3

'#((4) (2) (3) (1))

Added in version 6.6.0.5 of package base.

Like vector-copy , vector-append , vector-set/copy , and vector-extend but constrained to work on vectors that are not impersonators.

Added in version 8.11.1.10 of package base.
Changed in version 8.12.0.10: Added vector*-extend .

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /