Strings are fixed-length sequences of characters. They can be created by calling constructor procedures, but they can also literally get entered at the REPL or in Scheme source files.
Strings always carry the information about how many characters they are composed of with them, so there is no special end-of-string character, like in C. That means that Scheme strings can contain any character, even the ‘#\nul’ character ‘\0’.
To use strings efficiently, you need to know a bit about how Guile implements them. In Guile, a string consists of two parts, a head and the actual memory where the characters are stored. When a string (or a substring of it) is copied, only a new head gets created, the memory is usually not copied. The two heads start out pointing to the same memory.
When one of these two strings is modified, as with
their common memory does get copied so that each string has its own
memory and modifying one does not accidentally modify the other as well.
Thus, Guile’s strings are ‘copy on write’; the actual copying of their
memory is delayed until one string is written to.
This implementation makes functions like
efficient in the common case that no modifications are done to the
If you do know that your strings are getting modified right away, you
substring/copy instead of
function performs the copy immediately at the time of creation. This
is more efficient, especially in a multi-threaded program. Also,
substring/copy can avoid the problem that a short substring
holds on to the memory of a very large original string that could
otherwise be recycled.
If you want to avoid the copy altogether, so that modifications of one
string show up in the other, you can use
strings created by this procedure are called mutation sharing
substrings since the substring and the original string share
modifications to each other.
If you want to prevent modifications, use
Guile provides all procedures of SRFI-13 and a few more.
|• String Syntax||Read syntax for strings.|
|• String Predicates||Testing strings for certain properties.|
|• String Constructors||Creating new string objects.|
|• List/String Conversion||Converting from/to lists of characters.|
|• String Selection||Select portions from strings.|
|• String Modification||Modify parts or whole strings.|
|• String Comparison||Lexicographic ordering predicates.|
|• String Searching||Searching in strings.|
|• Alphabetic Case Mapping||Convert the alphabetic case of strings.|
|• Reversing and Appending Strings||Appending strings to form a new string.|
|• Mapping Folding and Unfolding||Iterating over strings.|
|• Miscellaneous String Operations||Replicating, insertion, parsing, ...|
|• Representing Strings as Bytes||Encoding and decoding strings.|
|• Conversion to/from C|
|• String Internals||The storage strategy for strings.|