list-ref(3scm)
π list-ref β the manual, but make it a group chat π
okay so bestie, you're reading list-ref(3scm) from the Scheme Programmer's Manual, and honestly? this one's a little icon. it's the "gimme the *k*th thing outta this list" procedure. let's get into it π
β¨ NAME β¨
list-ref β access an element of a list. that's it. that's the whole vibe. she grabs one thing out of the list and hands it to you π«Ά
π LIBRARY π
depending on which Scheme you're living in, import it like this (comments are the original flavor, keep 'em):
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
πΈ SYNOPSIS πΈ
(list-ref list k)
π DESCRIPTION π
so basically: returns the kth element of list. main character behavior fr fr, she just reaches in and pulls out exactly the one you asked for β¨
π Circular lists π
okay this is the part where it gets spicy πΆοΈ β R7RS explicitly allows list to be circular. and R6RS also allows that, but it also limits the implementation to only checking that list is a chain of pairs whose length is at least k + 1. so like, R6RS is doing the bare minimum vibe check on it, no cap π
π RETURN VALUES π
returns a single value β one object from the list. just one. not a list of one. one object. periodt π
π§ͺ EXAMPLES π§ͺ
the classics, look:
(list-ref '(a b c d) 2) => c
(list-ref '(a b c d)
(exact (round 1.8)))
=> c
and now the fun one where we make a list eat its own tail ππ
(define lst (list 'a 'b 'c 'd))
(set-cdr! (cdddr lst) lst) ;lst is now circular
(list-ref lst 4)
=> a
like?? she's circular and list-ref still ate that. iconic π
π₯
π¦ COMPATIBILITY π¦
except for different error behavior, this procedure works the same everywhere. that's it. she travels well, she's lowkey universal π
π ERRORS π
this procedure can raise exceptions with the following condition types β heads up bestie, here's where it can go sideways:
&assertion(R6RS) β the wrong number of arguments was passed or an argument was outside its domain. in particular, list must be a list whose length is at least k + 1, and k must be an exact nonnegative integer. no shortcut, no wiggle room π- 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, that's the actual wording, and yes it's giving horror movie)
fr fr don't just assume it'll politely error. R7RS implementations are allowed to go feral.
π SEE ALSO π
go touch some grass (or these other pages): car(3scm), vector-ref(3scm), assoc(3scm) π
β STANDARDS β
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β she's certified in all of them, an overachiever fr πβ¨
π§ HISTORY π§
this procedure first appeared in R2RS. a true elder, an OG, we respect her ππ
π«Ά 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/. go say hi to the maintainers, they're the real MVPs π
and that's a wrap bestie!! this page is dated 2023-01-29 and it's the list-ref(3scm) entry from the Scheme Programmer's Manual β go forth and index things responsibly πΈπ
β¨
π¦ 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