Next: , Previous: TCP & UDP sockets, Up: Libraries


6.7 Common-Lisp-style formatting

Scheme48 provides a simple Common-Lisp-style format facility in the formats structure. It does not provide nearly as much functionality as Common Lisp, however: the considerable complexity of Common Lisp's format was deliberately avoided because it was deemed inconsistent with Scheme48's design goals. Scheme48's format is suitable for most simple purposes, anyhow.

— procedure: format port control-string argument ... –> unspecified or string

Prints control-string to port. If, anywhere in control-string, the character ~ (tilde) occurs, the following character determines what to print in the place of the tilde and following character. Some formatting directives consume arguments from argument .... Formatting directive characters are case-insensitive. If port is #t, the output is printed to to the value of (current-output-port); if port is false, the output is collected in a string and returned.

The complete list of formatting directives:

~~
Prints a single ~ (tilde), and does not consume an argument.
~A
Consumes and prints the first remaining argument with display. (`A'ny)
~D
Consumes and prints the first remaining argument as a decimal number using number->string. (`D'ecimal)
~S
Consumes and prints the first remaining argument with write. (`S'-expression)
~%
Prints a newline with newline.
~&
Prints a newline with newline, unless it can be determined that a newline was immediately previously printed to port (see I/O extensions).
~?
Recursively formats. The first remaining argument is consumed and must be another control string; the argument directly thereafter is also consumed, and it must be a list of arguments corresponding with that control string. The control string is formatted with those arguments using format.

Format examples:

          (format #t "Hello, ~A!~%" "world")
              -| Hello, world!
              -|
          
          (format #t "Hello~?~S~%" "~A world" '(#\,) '!)
              -| Hello, world!
              -|
          
          (format #f "~A~A ~A." "cH" "uMBLE" "spuZz")
              => "cHuMBLE spuZz."
          
          (let ((x 10) (y .1))
            (format #t "x: ~D~%~&y: ~D~%~&" x y))
              -| x: 10
              -| y: .1