Node:Formatted Output, Next:, Previous:Pretty Printing, Up:Top

43 Formatted Output

Outputting messages or other texts which are composed of literal strings, variable contents, newlines and other formatting can be cumbersome, when only the standard procedures like display, write and newline are available. Additionally, one often wants to collect the output in strings. With the standard routines, the user is required to set up a string port, add this port as a parameter to the output procedure calls and then retrieve the resulting string from the string port.

The format procedure, to be found in module (ice-9 format), can do all this, and even more. If you are a C programmer, you can think of this procedure as Guile's fprintf.

format destination format-string args ... Scheme Procedure
The first parameter is the destination, it determines where the output of format will go.
Send the formatted output to the current output port and return #t.
Return the formatted output as a string.
Any number value
Send the formatted output to the current error port and return #t.
A valid output port
Send the formatted output to the port destination and return #t.

The second parameter is the format string. It has a similar function to the format string in calls to printf or fprintf in C. It is output to the specified destination, but all escape sequences are replaced by the results of formatting the corresponding sequence.

Note that escape sequences are marked with the character ~ (tilde), and not with a % (percent sign), as in C.

The escape sequences in the following table are supported. When there appears "corresponding arg', that means any of the additional arguments, after dropping all arguments which have been used up by escape sequences which have been processed earlier. Some of the format characters (the characters following the tilde) can be prefixed by :, @, or :@, to modify the behaviour of the format character. How the modified behaviour differs from the default behaviour is described for every character in the table where appropriate.

Output a single ~ (tilde) character.
Output a newline character, thus advancing to the next output line.
Start a new line, that is, output a newline character if not already at the start of a line.
Output a single space character.
Output a single tabulator character.
Output a page separator (formfeed) character.
Advance to the next tabulator position.
Pretty-print the corresponding arg.
Output the corresponding arg like display.
Output the corresponding arg like write.
Output the corresponding arg as a decimal number.
Output the corresponding arg as a hexadecimal number.
Output the corresponding arg as an octal number.
Output the corresponding arg as a binary number.
Output the corresponding arg as a number word, e.g. 10 prints as ten. If prefixed with :, tenth is printed, if prefixed with :@, Roman numbers are printed.
Output the corresponding arg as a fixed format floating point number, such as 1.34.
Output the corresponding arg in exponential notation, such as 1.34E+0.
This works either like ~f or like ~e, whichever produces less characters to be written.
Like ~f, but only with two digits after the decimal point.
Output the corresponding arg as a complex number.
Output the corresponding arg as a character. If prefixed with @, it is printed like with write. If prefixed with :, control characters are treated specially, for example #\newline will be printed as ^J.
"Plural". If the corresponding arg is 1, nothing is printed (or y if prefixed with @ or :@), otherwise s is printed (or ies if prefixed with @ or :@).
~?, ~k
Take the corresponding argument as a format string, and the following argument as a list of values. Then format the values with respect to the format string.
Flush the output to the output port.
~#\newline (tilde-newline)
Continuation lines.
Argument jumping. Navigate in the argument list as specified by the corresponding argument. If prefixed with :, jump backwards in the argument list, if prefixed by :@, jump to the parameter with the absolute index, otherwise jump forward in the argument list.
Case conversion begin. If prefixed by :, the following output string will be capitalized, if prefixed by @, the first character will be capitalized, if prefixed by :@ it will be upcased and otherwise it will be downcased. Conversion stops when the "Case conversion end" ~)sequence is encountered.
Case conversion end. Stop any case conversion currently in effect.
Conditional begin.
Conditional separator.
Conditional end.
Iteration begin.
Iteration end.
Up and out.
Character parameter.
~0 ... ~9, ~-, ~+
Numeric parameter.
Variable parameter from next argument.
Parameter is number of remaining args. The number of the remaining arguments is prepended to the list of unprocessed arguments.
Parameter separators.
Inquiry message. Insert a copyright message into the output.

If any type conversions should fail (for example when using an escape sequence for number output, but the argument is a string), an error will be signalled.

You may have noticed that Guile contains a format procedure even when the module (ice-9 format) is not loaded. The default format procedure does not support all escape sequences documented in this chapter, and will signal an error if you try to use one of them. The reason for providing two versions of format is that the full-featured module is fairly large and requires some time to get loaded. So the Guile maintainers decided not to load the large version of format by default, so that the start-up time of the interpreter is not unnecessarily increased.