vector-set!(3scm)

πŸ“¦ version 0-unstable-2024-02, in nixpkgs unstable

πŸ’… vector-set! (3scm) β€” from the Scheme Programmer's Manual, bestie ✨

okay so. we're doing vector-set! today and honestly? she's iconic. small procedure, big main character energy. this page lives in the Scheme Programmer's Manual (that's the 3scm part, iykyk) and she's here to help you poke at vectors. let's go 🫢


🌸 NAME 🌸

vector-set! β€” modify an element of a vector. that's it. that's the whole gig. she changes one little slot and dips πŸ’…


πŸ“š LIBRARY πŸ“š

import her from whichever flavour of Scheme you're living in, no judgement:

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

✨ SYNOPSIS ✨

(vector-set! vector k obj)

three arguments, one very important job πŸŽ€


πŸ’– DESCRIPTION πŸ’–

Stores obj in element k of vector. that's the whole thing, no cap. you point at a slot, you drop a new value in it. slay πŸ”₯


🧠 IMPLEMENTATION NOTES 🧠

okay this is the juicy part, pay attention bestie πŸ‘‘

Constants in the source code are not supposed to be modified by the program β€” that's the rule β€” but programs may still be able to do so anyway. like, the vibes are illegal but the door is unlocked 😭

A compiler that creates executable files can place the constants in read-only pages, where the processor will automatically report attempts to mutate them. so the hardware itself snitches on you. iconic behaviour fr fr πŸ’€

Interpreters and online compilers normally skip this step and tend to not detect mutation of constants. they simply don't check. living their truth πŸ™ˆ

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. lots of ways to say "don't touch" πŸ’…

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. do NOT trust a single REPL vibe check, bestie, she lies 😭

πŸŽ€ RETURN VALUES πŸŽ€

  • R6RS β€” Returns unspecified values. (plural! she's fancy)
  • R7RS β€” Returns an unspecified value. (singular, humble)

either way, don't count on getting anything useful back πŸ¦‹


🌈 EXAMPLES 🌈

first one, the classic. mutate a slot in a real live vector:

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

Anna stays, slot 1 gets a whole new duo. periodt.

second one, the chaotic one β€” this is mutation of a constant vector. Constants are allowed to be immutable, but implementations do not always check this. It should raise an &assertion exception:

;; 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

so she says unspecified, but she should be throwing hands with &assertion. chaotic queen πŸ’€


πŸ¦‹ APPLICATION USAGE πŸ¦‹

ok so the specification of vectors does not actually require constant access time, but in practice they are always implemented as flat arrays. always. no cap.

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. so if you tried this with lists you'd be sitting there waiting forever and the vibe would be ruined 😭


🌸 COMPATIBILITY 🌸

This procedure works the same everywhere. universal queen. no notes. πŸ’–


πŸ’€ 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. read that again: exact, nonnegative, less than the length. all three. don't test her πŸ™ˆ
  • 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. yes, "fail catastrophically" is the actual wording and yes I'm screaming πŸ˜­πŸ’€

πŸ‘€ SEE ALSO πŸ‘€

the whole friend group honestly 🫢


πŸ‘‘ STANDARDS πŸ‘‘

R4RS, IEEE Scheme, R5RS, R6RS, R7RS β€” she's been certified by literally everyone ⭐


🧚 HISTORY 🧚

This procedure first appeared in R2RS, where the vector notation was optional. yeah. optional. the vector notation was optional. can you imagine 😭 ancient times fr


πŸ’œ 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/. love them, thank them, go look at the repo 🌷


πŸ“ BUGS πŸ“

Programs should not modify constants.

that's it. that's the bug section. one line. she said what she said and she's right πŸ’…


πŸŽ€βœ¨ and that's the page bestie!! written up in the manual on 2023-01-28, coming to you straight from the scheme-manpages project (that's the source + version at the bottom of the original, folded in all cute for you). go set some vector elements, but like... responsibly. mwah πŸ’‹πŸŒˆβ­
πŸ“¦ where this page comes from