vector-ref(3scm)

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

πŸ’… 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, k must be an exact nonnegative integer less than the length of vector. 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