Scheme vectors
last modified November 24, 2026 Guile 3.0
A vector is a fixed-length sequence of values that are reached by
an index in constant time. Where a list must be walked from the start, a
vector goes straight to any element, so vectors suit tables, buffers, and
grids. The data types article introduced
vector-ref and vector-set!, and the
tail calls and iteration article looped over
a vector with do. This article covers the rest of the core
procedures and the SRFI 43 vector library. The examples run with GNU Guile
3.0.7.
Creating vectors
vector builds a vector from its arguments, and
make-vector creates one of a given length, filled with an
initial value. A vector literal is written #(...); it is a
constant, so it must not be modified.
(define primes (vector 2 3 5 7 11)) (define zeros (make-vector 4 0)) (define blank (make-vector 2)) (define days #(mon tue wed thu fri)) (write primes) (newline) (write zeros) (newline) (write blank) (newline) (write (list (vector-length primes) (vector-length days) (vector-length #()))) (newline) (write (vector 'a "b" #\c 4.5 (list 1 2) (vector 6))) (newline) (write (list (vector? days) (vector? (list 1 2)))) (newline)
Without the fill argument, make-vector leaves the elements
unspecified, and Guile prints them as #<unspecified>. The
elements of a vector literal are not evaluated, so #(mon tue ...)
holds symbols, as if quoted. vector-length returns the number of
elements; the empty vector #() has none. A vector may hold values
of different types, including lists and other vectors.
$ guile creating.scm #(2 3 5 7 11) #(0 0 0 0) #(#<unspecified> #<unspecified>) (5 5 0) #(a "b" #\c 4.5 (1 2) #(6)) (#t #f)
Reading and writing elements
vector-ref reads the element at an index and
vector-set! replaces it. Indexes run from 0 to the length minus
one. The next program keeps the state of six seats in a vector.
(define seats (make-vector 6 'free))
(define (book! seat)
(if (eq? (vector-ref seats seat) 'free)
(begin
(vector-set! seats seat 'taken)
#t)
#f))
(write (list (book! 2) (book! 5) (book! 2)))
(newline)
(write seats)
(newline)
(define (count-free)
(let loop ((i 0) (n 0))
(if (= i (vector-length seats))
n
(loop (+ i 1)
(if (eq? (vector-ref seats i) 'free) (+ n 1) n)))))
(write (count-free))
(newline)
(write (vector-ref seats (- (vector-length seats) 1)))
(newline)
All seats start as free. book! marks a free seat as
taken and returns #t, or returns #f when the seat is
already taken, so the second booking of seat 2 fails. count-free
walks the indexes with a named let. The last element is at index
(- (vector-length seats) 1).
$ guile access.scm (#t #t #f) #(free free taken free free taken) 4 taken
Index out of range
An index outside the vector is an error. The compiler cannot see it, so the
program fails only when the bad vector-ref runs.
(define seats (make-vector 6 'free)) (write (vector-ref seats 5)) (newline) (write (vector-ref seats 6)) (newline)
Index 5 is the last valid one. Index 6 stops the program with an error that names the procedure, the argument position, and the bad value. Guile also prints a backtrace, which is omitted here.
$ guile bounds.scm free bounds.scm:5:7: In procedure vector-ref: Argument 2 out of range: 6
Converting to and from lists
vector->list and list->vector convert between
the two sequence types, so the list procedures from the earlier articles can
work on vectors too. vector-copy makes a new vector with the same
elements, and vector-fill! sets every element to one value.
(define temps (vector 18 21 17 25 19)) (write (vector->list temps)) (newline) (write (list->vector (list 'x 'y 'z))) (newline) (define (vector-filter keep? vec) (list->vector (filter keep? (vector->list vec)))) (write (vector-filter (lambda (t) (>= t 19)) temps)) (newline) (write (apply max (vector->list temps))) (newline) (define backup (vector-copy temps)) (vector-fill! temps 0) (write temps) (newline) (write backup) (newline) (write (list (equal? backup (vector 18 21 17 25 19)) (eq? backup temps))) (newline)
vector-filter converts the vector to a list, keeps the
temperatures of at least 19 degrees with filter, and converts the
result back. apply passes the elements as arguments to
max. backup is a separate copy, so filling
temps with zeros does not change it. equal?
compares vectors element by element, while eq? tells that the two
are different objects.
$ guile converting.scm (18 21 17 25 19) #(x y z) #(21 25 19) 25 #(0 0 0 0 0) #(18 21 17 25 19) (#t #f)
SRFI 43: mapping
The core of Guile has only a few vector procedures; vector-map,
for example, is unbound in a plain script. SRFI 43, the vector library, adds
the rest and loads without warnings. Its vector-map and
vector-for-each pass the index to the procedure before the
elements.
(use-modules (srfi srfi-43)) (define prices (vector 12.5 8.0 3.25 20.0)) (write (vector-map (lambda (i p) (* p 2)) prices)) (newline) (write (vector-map (lambda (i p) (list i p)) prices)) (newline) (write (vector-map (lambda (i a b) (+ a b)) (vector 1 2 3) (vector 10 20 30))) (newline) (vector-for-each (lambda (i p) (display (+ i 1)) (display ". ") (display p) (newline)) prices) (define totals (vector 0 0 0)) (vector-map! (lambda (i x) (+ x (* i 100))) totals) (write totals) (newline)
The first vector-map ignores the index and doubles each price;
the second pairs each index with its element. With two vectors, the procedure
receives the index and one element of each vector. vector-for-each
uses the index to number the lines of a list. vector-map!
replaces the elements in place instead of building a new vector.
$ guile mapping.scm #(25.0 16.0 6.5 40.0) #((0 12.5) (1 8.0) (2 3.25) (3 20.0)) #(11 22 33) 1. 12.5 2. 8.0 3. 3.25 4. 20.0 #(0 100 200)
SRFI 43: searching
SRFI 43 has procedures that search a vector or test its elements.
vector-index returns the index of the first element that
satisfies a predicate, and vector-binary-search finds a value in
a sorted vector.
(use-modules (srfi srfi-43)) (define readings (vector 4 9 15 2 23 8 15)) (write (vector-index (lambda (x) (> x 10)) readings)) (newline) (write (vector-index (lambda (x) (> x 100)) readings)) (newline) (write (vector-count (lambda (i x) (even? x)) readings)) (newline) (write (list (vector-any negative? readings) (vector-every positive? readings))) (newline) (define (vector-position value vec) (vector-index (lambda (x) (= x value)) vec)) (write (vector-position 15 readings)) (newline) (define sorted (vector 2 4 8 9 15 15 23)) (define (compare a b) (- a b)) (write (vector-binary-search sorted 9 compare)) (newline) (write (vector-binary-search sorted 10 compare)) (newline)
The first reading above 10 is at index 2, and no reading is above 100, so
vector-index returns #f. Unlike
vector-index, vector-count passes the index to its
predicate as well. vector-any and vector-every test
whether some or all elements satisfy a predicate. vector-position
finds a value with an equality test. vector-binary-search needs a
comparison procedure that returns a negative number, zero, or a positive
number; it halves the search range at each step and returns the index of the
value, or #f when the value is absent.
$ guile searching.scm 2 #f 3 (#f #t) 2 3 #f
SRFI 43: building and folding
vector-unfold creates a vector by calling a procedure for each
index, vector-fold combines the elements into one value, and
vector-append and the SRFI 43 vector-copy build new
vectors from existing ones.
(use-modules (srfi srfi-43))
(define squares (vector-unfold (lambda (i) (* i i)) 6))
(write squares)
(newline)
(write (vector-fold (lambda (i sum x) (+ sum x)) 0 squares))
(newline)
(write (vector-fold (lambda (i best x) (max best x)) 0 (vector 3 17 9)))
(newline)
(write (vector-append (vector 1 2) (vector 3) (vector 4 5)))
(newline)
(write (vector-copy squares 2 5))
(newline)
(write (vector-copy squares 4 8 'none))
(newline)
(define (vector-slices vec size)
(let loop ((start 0) (slices '()))
(if (>= start (vector-length vec))
(reverse slices)
(loop (+ start size)
(cons (vector-copy vec start
(min (+ start size) (vector-length vec)))
slices)))))
(write (vector-slices (vector 'a 'b 'c 'd 'e 'f 'g) 3))
(newline)
vector-unfold fills six elements with the squares of their
indexes. vector-fold passes the index, the accumulated value,
and the element, so it can sum the squares or find the largest element.
vector-copy accepts a start and an end index and copies only that
part; when the end lies beyond the vector, the optional fill value pads the
copy. vector-slices uses it to cut a vector into pieces of three
elements, where the last piece is shorter.
$ guile building.scm #(0 1 4 9 16 25) 55 17 #(1 2 3 4 5) #(4 9 16) #(16 25 none none) (#(a b c) #(d e f) #(g))
Changing a vector in place
Because vectors are mutable, many operations can rearrange the elements
without allocating a new vector. SRFI 43 provides vector-swap!
and vector-reverse!, and its vector-fill! accepts a
range.
(use-modules (srfi srfi-43))
(define letters (vector 'a 'b 'c 'd 'e))
(vector-swap! letters 0 4)
(write letters)
(newline)
(vector-reverse! letters)
(write letters)
(newline)
(define (rotate-left! vec)
(let ((first (vector-ref vec 0))
(n (vector-length vec)))
(do ((i 0 (+ i 1)))
((= i (- n 1)))
(vector-set! vec i (vector-ref vec (+ i 1))))
(vector-set! vec (- n 1) first)))
(define queue (vector 1 2 3 4 5))
(rotate-left! queue)
(write queue)
(newline)
(rotate-left! queue)
(write queue)
(newline)
(define marks (vector 0 0 0 0 0 0 0 0))
(vector-fill! marks 1 2 5)
(write marks)
(newline)
vector-swap! exchanges the first and the last letter, and
vector-reverse! reverses the whole vector. rotate-left!
moves every element one position to the left with a do loop and
puts the first element at the end. The ranged vector-fill! sets
the elements from index 2 up to, but not including, index 5.
$ guile inplace.scm #(e b c d a) #(a d c b e) #(2 3 4 5 1) #(3 4 5 1 2) #(0 0 1 1 1 0 0 0)
Matrices
A matrix can be stored as a vector of rows, where each row is a vector. The
rows must be created separately: make-vector puts the same fill
value into every element, and when that value is a vector, all rows share it.
(define wrong (make-vector 3 (make-vector 3 0)))
(vector-set! (vector-ref wrong 0) 0 9)
(write wrong)
(newline)
(define (make-matrix rows cols init)
(let ((m (make-vector rows)))
(do ((r 0 (+ r 1)))
((= r rows) m)
(vector-set! m r (make-vector cols init)))))
(define (matrix-ref m r c) (vector-ref (vector-ref m r) c))
(define (matrix-set! m r c x) (vector-set! (vector-ref m r) c x))
(define table (make-matrix 3 4 0))
(do ((r 0 (+ r 1)))
((= r 3))
(do ((c 0 (+ c 1)))
((= c 4))
(matrix-set! table r c (* (+ r 1) (+ c 1)))))
(for-each (lambda (row)
(write row)
(newline))
(vector->list table))
(write (matrix-ref table 2 3))
(newline)
In wrong, the three elements refer to a single row vector, so
setting one element changes all three rows. make-matrix creates a
new row for each index. matrix-ref and matrix-set!
hide the two-step access, and the nested do loops fill the table
with the products of the row and column numbers.
$ guile matrix.scm #(#(9 0 0) #(9 0 0) #(9 0 0)) #(1 2 3 4) #(2 4 6 8) #(3 6 9 12) 12
A histogram
A vector indexed by small integers is a natural counter table. The program below counts the digits in a string, using the digit value as the index.
(define (digit-histogram text)
(let ((counts (make-vector 10 0)))
(string-for-each
(lambda (c)
(when (char-numeric? c)
(let ((d (- (char->integer c) (char->integer #\0))))
(vector-set! counts d (+ 1 (vector-ref counts d))))))
text)
counts))
(define counts (digit-histogram "Call 0905 123 456 or 02 5937 1100."))
(write counts)
(newline)
(define (bar n)
(make-string n #\*))
(do ((d 0 (+ d 1)))
((= d 10))
(unless (zero? (vector-ref counts d))
(display d)
(display " ")
(display (bar (vector-ref counts d)))
(newline)))
digit-histogram creates ten counters, one for each digit, and
increments the counter of every digit it finds in the text. The digit value is
the code of the character minus the code of #\0, as explained in
the characters article. The loop prints a
bar of stars for each digit that occurs; digit 8 does not occur and is
skipped by unless.
$ guile histogram.scm #(5 3 2 2 1 3 1 1 0 2) 0 ***** 1 *** 2 ** 3 ** 4 * 5 *** 6 * 7 * 9 **
Source
Vectors — GNU Guile Reference Manual
In this article we have created vectors, read and replaced their elements, met the index error, converted vectors to and from lists, copied and filled them, used the SRFI 43 library to map, search, fold, build, and rearrange vectors, stored a matrix as a vector of rows, and counted digits in a histogram. The pairs and lists article covers the other main sequence type, and the bytevectors article covers vectors of raw bytes.
Author
List all Scheme tutorials.