4.11 Vectors
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.
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 reading vectors and Printing Vectors for information on printing vectors.
procedure
(make-vector size [v]) → vector?
size : exact-nonnegative-integer? v : any/c = 0
procedure
(vector-immutable v ...) →
(and/c vector? immutable?) v : any/c
procedure
vec : vector?
procedure
(vector-ref vec pos) → any/c
vec : vector? pos : exact-nonnegative-integer?
procedure
(vector-set! vec pos v) → void?
vec : (and/c vector? (not/c immutable?)) pos : exact-nonnegative-integer? v : any/c
procedure
(vector->list vec) → list?
vec : vector?
procedure
(list->vector lst) → vector?
lst : list?
procedure
(vector->immutable-vector vec) → (and/c vector? immutable?)
vec : vector?
procedure
(vector-fill! vec v) → void?
vec : (and/c vector? (not/c immutable?)) v : any/c
procedure
(vector-copy! dest dest-start src [ src-start src-end]) → void? dest : (and/c vector? (not/c immutable?)) dest-start : exact-nonnegative-integer? src : vector? src-start : exact-nonnegative-integer? = 0 src-end : exact-nonnegative-integer? = (vector-length src)
> (define v (vector 'A 'p 'p 'l 'e))
> (vector-copy! v 4 #(y))
> (vector-copy! v 0 v 3 4)
> v '#(l p p l y)
procedure
(vector->values vec [start-pos end-pos]) → any
vec : vector? start-pos : exact-nonnegative-integer? = 0 end-pos : exact-nonnegative-integer? = (vector-length vec)
procedure
(build-vector n proc) → vector?
n : exact-nonnegative-integer? proc : (exact-nonnegative-integer? . -> . any/c)
> (build-vector 5 add1) '#(1 2 3 4 5)
4.11.1 Additional Vector Functions
(require racket/vector) | package: base |
procedure
(vector-set*! vec pos v ... ...) → void?
vec : (and/c vector? (not/c immutable?)) pos : exact-nonnegative-integer? v : any/c
procedure
(vector-map proc vec ...+) → vector?
proc : procedure? vec : vector?
> (vector-map + #(1 2) #(3 4)) '#(4 6)
procedure
(vector-map! proc vec ...+) → vector?
proc : procedure? vec : (and/c vector? (not/c immutable?))
> (define v (vector 1 2 3 4))
> (vector-map! add1 v) '#(2 3 4 5)
> v '#(2 3 4 5)
procedure
(vector-append vec ...) → vector?
vec : vector?
> (vector-append #(1 2) #(3 4)) '#(1 2 3 4)
procedure
(vector-take vec pos) → vector?
vec : vector? pos : exact-nonnegative-integer?
> (vector-take #(1 2 3 4) 2) '#(1 2)
procedure
(vector-take-right vec pos) → vector?
vec : vector? pos : exact-nonnegative-integer?
> (vector-take-right #(1 2 3 4) 2) '#(3 4)
procedure
(vector-drop vec pos) → vector?
vec : vector? pos : exact-nonnegative-integer?
> (vector-drop #(1 2 3 4) 2) '#(3 4)
procedure
(vector-drop-right vec pos) → vector?
vec : vector? pos : exact-nonnegative-integer?
> (vector-drop-right #(1 2 3 4) 2) '#(1 2)
procedure
(vector-split-at vec pos) →
vector? vector? vec : vector? pos : exact-nonnegative-integer?
(values (vector-take vec pos) (vector-drop vec pos))
except that it can be faster.
> (vector-split-at #(1 2 3 4 5) 2)
'#(1 2)
'#(3 4 5)
procedure
(vector-split-at-right vec pos) →
vector? vector? vec : vector? pos : exact-nonnegative-integer?
(values (vector-take-right vec pos) (vector-drop-right vec pos))
except that it can be faster.
> (vector-split-at-right #(1 2 3 4 5) 2)
'#(1 2 3)
'#(4 5)
procedure
(vector-copy vec [start end]) → vector?
vec : vector? start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (vector-length v)
> (vector-copy #(1 2 3 4)) '#(1 2 3 4)
> (vector-copy #(1 2 3 4) 3) '#(4)
> (vector-copy #(1 2 3 4) 2 3) '#(3)
procedure
(vector-filter pred vec) → vector?
pred : procedure? vec : vector?
> (vector-filter even? #(1 2 3 4 5 6)) '#(2 4 6)
procedure
(vector-filter-not pred vec) → vector?
pred : procedure? vec : vector?
> (vector-filter-not even? #(1 2 3 4 5 6)) '#(1 3 5)
procedure
(vector-count proc vec ...+) → exact-nonnegative-integer?
proc : procedure? vec : vector?
> (vector-count even? #(1 2 3 4 5)) 2
> (vector-count = #(1 2 3 4 5) #(5 4 3 2 1)) 1
> (vector-argmin car #((3 pears) (1 banana) (2 apples))) '(1 banana)
> (vector-argmin car #((1 banana) (1 orange))) '(1 banana)
> (vector-argmax car #((3 pears) (1 banana) (2 apples))) '(3 pears)
> (vector-argmax car #((3 pears) (3 oranges))) '(3 pears)
procedure
(vector-member v lst) → (or/c natural-number/c #f)
v : any/c lst : vector?
> (vector-member 2 (vector 1 2 3 4)) 1
> (vector-member 9 (vector 1 2 3 4)) #f
procedure
(vector-memv v vec) → (or/c natural-number/c #f)
v : any/c vec : vector?
> (vector-memv 2 (vector 1 2 3 4)) 1
> (vector-memv 9 (vector 1 2 3 4)) #f
procedure
(vector-memq v vec) → (or/c natural-number/c #f)
v : any/c vec : vector?
> (vector-memq 2 (vector 1 2 3 4)) 1
> (vector-memq 9 (vector 1 2 3 4)) #f