vector-ref(3scm)
π vector-ref β get that one element out of a vector (3scm)
Okay bestie, so this is a page from the Scheme Programmer's Manual and today's main character is vector-ref, the little procedure that reaches into a vector and hands you exactly the element you asked for. No cap. β¨
πΈ NAME
vector-ref β access an element of a vector
π LIBRARY
Import it from whichever flavor of Scheme you're living in, queen:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
π SYNOPSIS
(vector-ref vector k)
π¦ DESCRIPTION
Returns the contents of element k of vector. That's it, that's the whole vibe. You point at a spot, it gives you what's sitting there. π
β RETURN VALUES
Returns a single value β one object, straight out of the vector. Just one. Not a list, not a surprise, one object. π«Ά
π₯ EXAMPLES
Look at her go:
(vector-ref '#(1 1 2 3 5 8 13 21) 5)
=> 8
Index 5 (zero-based, obviously, we're not monsters) gives you 8. Fibonacci-core. π
And then the flex version, where the index is computed at runtime:
(vector-ref '#(1 1 2 3 5 8 13 21)
(exact
(round (* 2 (acos -1)))))
=> 13
(* 2 (acos -1)) is literally 2Ο, rounded that's 6, and element 6 is 13. Math girlies stay winning. π§
π APPLICATION USAGE
So here's the tea: the spec of vectors doesn't actually require constant access time, but in practice they're always implemented as flat arrays, so it basically always is. Which means this procedure is the one you reach for in algorithms doing random-access operations on arrays. If you tried the same thing with lists, those algorithms would not have the same time complexity β different big-O, different life. π
π COMPATIBILITY
This procedure works the same everywhere. Every Scheme, every implementation, same behavior. Iconic, stable, unbothered. π
π 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,kmust be an exact nonnegative integer less than the length ofvector. Read that again, bestie: exact, nonnegative, and less than the length. All three. π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, catastrophically. Choose your own adventure, but one of the paths is a cliff. π
π SEE ALSO
π STANDARDS
R4RS, IEEE Scheme, R5RS, R6RS, R7RS
π§ HISTORY
This procedure first appeared in R2RS, where the vector notation was optional. Vintage behavior, honestly. β
π 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/.
ok that's the whole page, go forth and index responsibly π«Ά
signing off β this was vector-ref(3scm) from the Scheme Programmer's Manual, dated 2023-01-28, and yes she ate π
β¨
π¦ 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