substring(3scm)
π substring(3scm) β from the Scheme Programmer's Manual bestie π
okay so before we even start: this page lives in the Scheme Programmer's Manual, and it's about one (1) little procedure that is lowkey iconic and you use it constantly without even thinking. let's get into it β¨
πΈ NAME πΈ
substring β copy part of a string. that's it. that's the whole job. she's simple, she's reliable, she ate π
π LIBRARY π
import her from whichever flavour you're on, no cap:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme r5rs)) ;R7RS
(import (scheme base)) ;R7RS
β¨ SYNOPSIS β¨
(substring string start end)
three arguments, no more, no less. main character energy fr π«Ά
π DESCRIPTION π
so substring returns a newly allocated string, and that string is made from the characters of string starting at index start (inclusive, bestie β she's in) and ending at index end (exclusive, so end itself is not invited to the party π).
the rules, and you will respect them:
string must be a string, and start and end must be exact integer objects satisfying
0 β€ start β€ end β€ (string-length string)
like, that chain has to hold. no vibes-based index math, okay? π
and here's a cute little detail: if start = end, then the resulting empty string does not need to be newly allocated, as per eq?(3scm). so like, an empty string is still an empty string. she's reusable π
π§ IMPLEMENTATION NOTES π§
- Chez Scheme, Loko Scheme, (more?) β there is a unique empty string object.
(the "(more?)" is literally in the original, it's giving "we're still collecting data" π)
π RETURN VALUES π
returns a single string object. one and done. slay β
π EXAMPLES π
(substring "SCHEMER" 0 6) => "SCHEME"
see how she dropped the R? exclusive end, periodt π
π₯ COMPATIBILITY π₯
this procedure is the same in all RnRS Scheme revisions since R2RS. like, genuinely consistent across the whole timeline. no drama, no breaking changes, we love a stable queen π
π 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. i.e. you gave her something she cannot work with and she will let you know π- 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, "fail catastrophically" is the actual wording. so like... be normal about your indices please π
π¦ SEE ALSO π¦
π STANDARDS π
R4RS, IEEE Scheme, R5RS, R6RS, R7RS
certified by literally everyone, iykyk π
π§ HISTORY π§
this procedure was introduced in R2RS. earlier revisions of Scheme ran on MacLisp, which had a substr function that it in turn copied from PL/I. instead of the end argument, it had an optional length argument. so like, the ancestor of this function was measuring lengths instead of endpoints, and honestly? we've grown as a community π
π«Ά 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 whole page bestie β¨ no notes, no cap, she's iconic. this was substring(3scm) from the Scheme Programmer's Manual, dated 2022-03-28 β go forth and slice your strings 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