list-set!(3scm)
π list-set! β like, the one page where you get to change stuff in a list
okay so real talk, this is list-set!(3scm) from the Scheme Programmer's Manual, and honestly? she's a little icon. let's get into it π
β¨ NAME β¨
list-set! β modify an element in a list. yes bestie, that exclamation mark is doing work, it means she MUTATES π
π LIBRARY π
(import (scheme base)) ;R7RS
that's the import, no cap. (scheme base), and the comment says ;R7RS so you know where she comes from π¦
πΈ SYNOPSIS πΈ
(list-set! list k obj)
three little arguments, that's the whole vibe β¨
π DESCRIPTION π
So what does she actually do? She stores obj in element k of list. That's it. That's the whole thing. She takes element number k and she's like "nope, we're putting obj there now" π
π«Ά RETURN VALUES π«Ά
Returns an unspecified value. like, she did the thing, she's not gonna brag about it, she just hands you back ~vibes~ and you move on π
π EXAMPLES π
okay this first one is giving main character energy:
(let ((ls (list 'one 'two 'five!)))
(list-set! ls 2 'three)
ls)
=> (one two three)
look at that glow-up π five! got absolutely replaced by three and the list is thriving.
and then... the cautionary tale π
(list-set! '(0 1 2) 1 "oops")
error ; constant list should not be mutated
β οΈ bestie DO NOT. that's a constant list and she should not be mutated. you will get an error and it will be embarrassing. we do not touch constants. periodt.
π§ RATIONALE π§
This procedure was added in R7RS to round out the sequence operations. so like, she's the missing puzzle piece, the group chat member who finally joined, the one who completes the set π₯
π COMPATIBILITY π
This procedure is unique to R7RS. nobody else has her like that π
An alternative? sure, you could use set-car!(3scm) and list-tail(3scm) β that's the route if you're feeling old-school β but honestly, why would you when she's right here π¦
π ERRORS π
okay this is the serious part, lowkey:
It is an error if:
- the wrong number of arguments was passed, or
- an argument was outside its domain.
And in particular β this is the big one β it is an error if k is not a valid index of list. π¨
Now, what happens then is genuinely implementation-dependent, and the manpage says it straight: implementations may signal an error, may extend the procedure's domain of definition to include such arguments, or may fail catastrophically πππ
yeah. fail catastrophically. they really wrote that. no cap. be careful with your k bestie π
π SEE ALSO π
set-car!(3scm)β the other one, go say hi π«Ά
β STANDARDS β
R7RS β that's the standard, that's the whole standard, iconic behaviour β¨
π HISTORY π
This procedure first appeared in R7RS. her debut. her origin story. we love a queen who arrived fully formed π
πΈ 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/ π¦
and that's the page!! tysm for reading, go mutate something (not a constant tho, i'm literally begging you) πβ¨
β from the Scheme Programmer's Manual, dated 2023-08-13, page list-set!(3scm). mwah π
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms