string-ref(3scm)
π string-ref β get a character from a string π
okay bestie, welcome to the Scheme Programmer's Manual β¨ (yes that's the manual name, i'm folding it in here so it doesn't get lonely at the top). this one's about string-ref, and it's honestly so much cuter than it sounds, stick with me π«Ά
πΈ NAME πΈ
string-ref β get a character from a string. that's it. that's the whole job. iconic behavior honestly π
π LIBRARY π
like, you gotta import the right thing first, and the vibe depends on which standard you're living in:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
pick your fighter, they all work π
β¨ SYNOPSIS β¨
(string-ref string k)
π« DESCRIPTION π«
Returns character k of string, using zero-origin indexing β meaning the first character is at index 0, not 1. yes bestie, counting from zero, we been knew π§
- R6RS β implementors should make
string-refrun in constant time. should! it's a should, not a must, don't come for me π - R7RS β there is no requirement for this procedure to execute in constant time. it's giving "we're keeping our options open" π
π οΈ IMPLEMENTATION NOTES π οΈ
- Chibi Scheme β Chibi supports
O(1)string access if theSEXP_USE_STRING_INDEX_TABLEfeature is enabled. so like, flip that flag and you're speedrunning β
π RETURN VALUES π
Returns a single value, which is a character object. it's eqv?(3scm) to the one that was last placed in the string at index k β so the exact same character object you put in there, no copies, no cap π
π EXAMPLES π
(string-ref "abc" 1) => #\b
ate. no crumbs. π₯
π¦ APPLICATION USAGE π¦
This procedure is used to implement algorithms that read strings in non-linear access patterns β like, jumping around, main character energy, no order, just vibes. if your program reads strings at increasing indices though, you can use open-string-input-port(3scm) instead. (that's the chill sequential way, highly recommend if that's your thing π)
π RATIONALE π
The choice of R7RS to not require execution in constant time opens the possibility for implementations to use UTF-8 or UTF-16 as the internal string representation. so like, it's a trade-off bestie, and honestly? understandable π
π COMPATIBILITY π
This procedure is functionally the same in all RnRS revisions after R2RS. the differences are in:
- which characters an implementation can represent
- how errors are signalled
- the time complexity of doing the lookup
so the what is the same, the how is a whole personality difference π
π 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. raised ifkis not a valid index ofstring.- 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 π (yeah. that's a real quote. buckle up.)
bestie please don't pass a bad index. "fail catastrophically" is not a cute look, i'm begging π
π SEE ALSO π
string-set!(3scm)vector-ref(3scm)open-string-input-port(3scm)
go read those next, i'll wait π
β STANDARDS β
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β it's certified, it's legit, it's in all of them π
π°οΈ HISTORY π°οΈ
First introduced in R2RS. Scheme before R2RS, running on MacLISP, had a similar getchar function β but plot twist: it used one-origin indexing and worked on atomic symbols instead of character strings. so like, the ancestor of this function was a whole different creature π we've come so far π¦
π«Ά AUTHORS π«Ά
This page is part of the scheme-manpages project. It includes materials from the RnRS documents. more info lives at https://github.com/schemedoc/manpages/ β go show them love, they made this whole thing β¨
π BUGS π
It is easy to get the index validity check wrong. like, really easy. for example: GNU Guile 3.0.8 segfaults on evaluating (string-ref "" -1). segfaults!! on a one-liner!! that's not slay, that's a crash bestie ππ
ngl this whole page is a mood: constant-time should, constant-time not required, and one very famous Scheme that absolutely fumbled the boundary check. remember the vibes, remember the facts π
πΈ that's the whole glow-up! sourced from the scheme-manpages project, dated 2022-04-04, and the page itself is string-ref(3scm) β mwah, go read a string, character by character, responsibly πβ¨π«Ά
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms