vector-set! - modify an element of a vector

LIBRARY

(import (rnrs))                     ;R6RS
(import (rnrs base))                ;R6RS
(import (scheme r5rs))              ;R7RS
(import (scheme base))              ;R7RS

SYNOPSIS

(vector-set! vector k obj)

DESCRIPTION

Stores obj in element k of vector.

IMPLEMENTATION NOTES

Constants in the source code are not supposed to be modified by the program, but programs may still be able to do so. A compiler that creates executable files can place the constants in read-only pages, where the processor will automatically report attempts to mutate them. Interpreters and online compilers normally skip this step and tend to not detect mutation of constants. Implementations can also mark a vector as immutable using various strategies like special memory ranges, a special immutable vector tag or an immutable bit next to the length field.

Implementations may also do different things in different modes, so just testing at the REPL is not a reliable method to check if constants are always mutable or not in a particular implementation.

RETURN VALUES

R6RS
Returns unspecified values.
R7RS
Returns an unspecified value.

EXAMPLES

(let ((vec (vector 0 '(2 2 2 2) "Anna")))
   (vector-set! vec 1 '("Sue" "Sue"))
   vec)
            =>  #(0 ("Sue" "Sue") "Anna")

;; This example shows mutation of a constant vector. Constants are
;; allowed to be immutable, but implementations do not always check
;; this. It should raise an &assertion exception.
(vector-set! '#(0 1 2) 1 "doe")
            => unspecified
              ; should raise &assertion exception

APPLICATION USAGE

The specification of vectors does not actually require constant access time, but in practice they are always implemented as flat arrays. Therefore this procedure is used in algorithms that perform random-access operations on arrays. Such algorithms would not have the same time complexity if lists were used instead.

COMPATIBILITY

This procedure works the same everywhere.

ERRORS

This procedure can raise exceptions with the following condition types:
&assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain. In particular, vector should be a mutable vector, and k must be an exact nonnegative integer less than the length of vector.
R7RS
The assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically.

SEE ALSO

make-vector(3scm), vector-ref(3scm), set-car!(3scm)

STANDARDS

R4RS, IEEE Scheme, R5RS, R6RS, R7RS

HISTORY

This procedure first appeared in R2RS, where the vector notation was optional.

AUTHORS

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/.

BUGS

Programs should not modify constants.


Markup created by unroff 1.0sc,    March 04, 2023.