4.12 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. 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 reading vectors and Printing Vectors for information on printing vectors.
See also immutable-vector? and mutable-vector?.
procedure
(make-vector size [v]) → vector?
size : exact-nonnegative-integer? v : any/c = 0
> (make-vector 3 2) '#(2 2 2)
> (define v (make-vector 5 (box 3))) > v '#(#&3 #&3 #&3 #&3 #&3)
> (set-box! (vector-ref v 0) 7) > v '#(#&7 #&7 #&7 #&7 #&7)
This function takes time proportional to size.
procedure
(vector-immutable v ...) →
(and/c vector? immutable?) v : any/c
procedure
vec : vector?
This function takes constant time.
procedure
(vector-ref vec pos) → any/c
vec : vector? pos : exact-nonnegative-integer?
This function takes constant time.
procedure
(vector-set! vec pos v) → void?
vec : (and/c vector? (not/c immutable?)) pos : exact-nonnegative-integer? v : any/c
This function takes constant time.
procedure
vec : (and/c vector? (not/c impersonator?))
procedure
(vector*-ref vec pos) → any/c
vec : (and/c vector? (not/c impersonator?)) pos : exact-nonnegative-integer?
procedure
(vector*-set! vec pos v) → void?
vec : (and/c vector? (not/c immutable?) (not/c impersonator?)) pos : exact-nonnegative-integer? v : any/c
Added in version 6.90.0.15 of package base.
procedure
(vector-cas! vec pos old-v new-v) → boolean?
vec : (and/c vector? (not/c immutable?) (not/c impersonator?)) pos : exact-nonnegative-integer? old-v : any/c new-v : any/c
Added in version 6.11.0.2 of package base.
procedure
(vector->list vec) → list?
vec : vector?
This function takes time proportional to the size of vec.
procedure
(list->vector lst) → vector?
lst : list?
This function takes time proportional to the length of lst.
procedure
(vector->immutable-vector vec) → (and/c vector? immutable?)
vec : vector?
This function takes time proportional to the size of vec when vec is mutable.
procedure
(vector-fill! vec v) → void?
vec : (and/c vector? (not/c immutable?)) v : any/c
This function takes time proportional to the size of vec.
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)
This function takes time proportional to (- src-end src-start).
> (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)
This function takes time proportional to the size of 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.12.1 Additional Vector Functions
(require racket/vector) | package: base |
procedure
(vector-empty? v) → boolean?
v : vector?
Added in version 7.4.0.4 of 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) 1) '#(1 2 3)
> (vector-drop-right #(1 2 3 4) 3) '#(1)
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-set/copy vec pos val) → vector?
vec : vector? pos : exact-nonnegative-integer? val : any/c
> (vector-set/copy #(1 2 3) 0 'x) '#(x 2 3)
> (vector-set/copy #(1 2 3) 2 'x) '#(1 2 x)
Added in version 8.11.1.10 of package base.
procedure
(vector-extend vec new-size [val]) → vector?
vec : vector? new-size : (and/c exact-nonnegative-integer? (>=/c (vector-length vec))) val : any/c = 0
> (vector-extend #(1 2 3) 10) '#(1 2 3 0 0 0 0 0 0 0)
> (vector-extend #(1 2 3) 10 #f) '#(1 2 3 #f #f #f #f #f #f #f)
> (vector-extend #(1 2 3) 3 #f) '#(1 2 3)
Added in version 8.12.0.10 of package base.
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 vec [is-equal?]) → (or/c natural-number/c #f)
v : any/c vec : vector? is-equal? : (-> any/c any/c any/c) = equal?
> (vector-member 2 (vector 1 2 3 4)) 1
> (vector-member 9 (vector 1 2 3 4)) #f
> (vector-member 1.0 (vector 1 2 3 4) =) 0
Changed in version 8.15.0.1 of package base: Added the is-equal? argument.
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
procedure
(vector-sort vec less-than? [ start end #:key key #:cache-keys? cache-keys?]) → vector? vec : vector? less-than? : (any/c any/c . -> . any/c) start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (vector-length vec) key : (or/c #f (any/c . -> . any/c)) = #f cache-keys? : boolean? = #f
> (define v1 (vector 4 3 2 1)) > (vector-sort v1 <) '#(1 2 3 4)
> v1 '#(4 3 2 1)
> (define v2 (vector '(4) '(3) '(2) '(1))) > (vector-sort v2 < 1 3 #:key car) '#((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 #:key key #:cache-keys? cache-keys?]) → void? vec : (and/c vector? (not/c immutable?)) less-than? : (any/c any/c . -> . any/c) start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (vector-length vec) key : (any/c . -> . any/c) = (λ (x) x) cache-keys? : boolean? = #f
> (define v1 (vector 4 3 2 1)) > (vector-sort! v1 <) > v1 '#(1 2 3 4)
> (define v2 (vector '(4) '(3) '(2) '(1))) > (vector-sort! v2 < 1 3 #:key car) > v2 '#((4) (2) (3) (1))
Added in version 6.6.0.5 of package base.
procedure
(vector*-copy vec [start end]) → vector?
vec : (and/c vector? (not/c impersonator?)) start : exact-nonnegative-integer? = 0 end : exact-nonnegative-integer? = (vector-length v)
procedure
(vector*-append vec ...) → vector?
vec : (and/c vector? (not/c impersonator?))
procedure
(vector*-set/copy vec pos val) → vector?
vec : (and/c vector? (not/c impersonator?)) pos : exact-nonnegative-integer? val : any/c
procedure
(vector*-extend vec pos [val]) → vector?
vec : (and/c vector? (not/c impersonator?)) pos : exact-nonnegative-integer? val : any/c = 0
Added in version 8.11.1.10 of package base.
Changed in version 8.12.0.10: Added vector*-extend.