string-for-each - call a procedure on each character in a string
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme base)) ;R7RS
(string-for-each proc string ...)
Applies proc element-wise to the characters of the strings for its side
effects, in order from the first characters to the last.
Proc is always called in the same dynamic environment as
string-for-each itself.
- R7RS
- If more than one string is given and not all strings have the same length,
this procedure terminates when the shortest string runs out. It is an
error for proc to mutate any of the strings.
The implementation must check the restrictions on proc to the extent
performed by applying it as described. An implementation may check whether
proc is an appropriate argument before applying it.
- R6RS
- Returns unspecified values.
- R7RS
- Returns an unspecified value.
(let ((v '()))
(string-for-each
(lambda (c) (set! v (cons (char->integer c) v)))
"abcde")
v)
=> (101 100 99 98 97)
This procedure is used for its side-effects, so this procedure does not show up
in purely functional programs. The characters are usually accumulated using
mutation of a variable or a data structure, or they are written to an output
port. Common usages are counting and conversion.
The R7RS variant of this procedure terminates early, which matches the R7RS
behavior for for-each(3scm).
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, this is raised if the strings do not all have the
same length, and when proc does not accept as many arguments are
there are strings.
- R7RS
- The assertions described above are errors. Further more, it is an error
for proc to mutate any of the strings. Implementations may signal
an error, extend the procedure's domain of definition to include such
arguments, or fail catastrophically.
The first Scheme report to have this procedure was R6RS. It was later also added
in R7RS. There is a variant in SRFI-13 that predates these two, but it has a
different signature: (string-for-each proc s
[start end])