call-with-string-output-port(3scm) Scheme Programmer's Manual call-with-string-output-port(3scm)

call-with-string-output-port - call a procedure with a string output port

(import (rnrs))                     ;R6RS
(import (rnrs io ports))            ;R6RS

(call-with-string-output-port proc)

Creates a textual output port that accumulates the characters written to it and calls proc with that output port as an argument. Whenever proc returns, a string consisting of all of the port's accumulated characters (regardless of the port's current position) is returned and the port is closed.

The port should support the port-position(3scm) and set-port-position!(3scm) operations.

The port may or may not have an associated transcoder; if it does, the transcoder is implementation-dependent.

Returns a single value; a string.

(call-with-string-output-port
  (lambda (p)
    (for-each (lambda (x)
                (display x p))
              '("Hello " world #\newline))))
        => "Hello world\n"

This procedure is commonly used when creating strings piecemeal.

This procedure is unique to R6RS. It is a wrapper around open-string-output-port(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, proc must be a procedure that accepts one argument.

call-with-string-output-port(3scm), open-string-output-port(3scm) open-string-input-port(3scm)

R6RS

This procedure is new to the reworked I/O in R6RS.

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

2023-02-15