The current version of SWI-Prolog provides two formatted write predicates. The first is writef/[1,2], which is compatible with Edinburgh C-Prolog. The second is format/[1,2], which is compatible with Quintus Prolog. We hope the Prolog community will once define a standard formatted write predicate. If you want performance use format/[1,2] as this predicate is defined in C. Otherwise compatibility reasons might tell you which predicate to use.
Escape sequences to generate a single special character:
|Output a newline character (see also nl/[0,1])|
|Output a line separator
(same as |
|Output a carriage-return character (ASCII 13)|
|Output the ASCII character TAB (9)|
|The character |
|The character |
|where <nnn> is an integer (1-3 digits) the character with character code <nnn> is output (NB : <nnn> is read as decimal)|
interpreted differently when character-escapes are in effect. See
Escape sequences to include arguments from Arguments. Each time a % escape sequence is found in Format the next argument from Arguments is formatted according to the specification.
|print/1 the next item (mnemonic: term)|
|write/1 the next item|
|writeq/1 the next item|
|Write the term, ignoring operators. See also write_term/2. Mnemonic: old Edinburgh display/1.|
the next item (identical to |
|Put the next item as a character (i.e., it is a character code)|
|Write the next item N times where N is the second item (an integer)|
|Write the next item as a String (so it must be a list of characters)|
|Perform a ttyflush/0 (no items used)|
|Write the next item Centered in N columns.|
|Write the next item Left justified in N columns.|
|Write the next item Right justified in N columns. N is a decimal number with at least one digit. The item must be an atom, integer, float or string.|
?- swritef(S, '%15L%w', ['Hello', 'World']). S = "Hello World"
swritef(String, Format, ).
format(Format) :- format(Format, ).'
Special sequences start with the tilde (
followed by an optional numeric argument, followed by a character
describing the action to be undertaken. A numeric argument is either a
sequence of digits, representing a positive decimal number, a sequence
representing the character code value of the character (only useful for
or a asterisk (
), in when the numeric
argument is taken from the next argument of the argument list, which
should be a positive integer.
Numeric conversion (
G) accept an arithmetic expression as argument. This is
introduced to handle rational numbers transparently (see
section 126.96.36.199. The floating
point conversions allow for unlimited precision for printing rational
numbers in decimal form.
~16rprints its argument hexadecimal. The argument should be in the range [2, ... , 36]. Lower case letters are used for digits above 9.
current_outputstream is inserted at this place. Goal is called in the module calling format/3. This option is not present in the original definition by Quintus, but supported by some other Prolog systems.
~tstatements between the tab stops. This space is padded with spaces by default. If an argument is supplied this is taken to be the character code of the character used for padding. This can be used to do left or right alignment, centering, distributing, etc. See also
~+to set tab stops. A tab stop is assumed at the start of each line.
~t's to be distributed between the previous and this tab stop.
format(' W', [Term, [numbervars(true)]]). This option is SWI-Prolog specific.
simple_statistics :- <obtain statistics> % left to the user format('~tStatistics~t~72|~n~n'), format('Runtime: ~`.t ~2f~34| Inferences: ~`.t ~D~72|~n', [RunT, Inf]), ....
Statistics Runtime: .................. 3.45 Inferences: .......... 60,345
library(backcomp). For example:
?- format(atom(A), '~D', ). A = '1,000,000'
~c(tilde, followed by some character) is found, the format derivatives will first check whether the user has defined a predicate to handle the format. If not, the built in formatting rules described above are used. Char is either an ASCII value, or a one character atom, specifying the letter to be (re)defined. Head is a term, whose name and arity are used to determine the predicate to call for the redefined formatting character. The first argument to the predicate is the numeric argument of the format command, or the atom
defaultif no argument is specified. The remaining arguments are filled from the argument list. The example below redefines
~nto produce Arg times return followed by linefeed (so a (Grr.) DOS machine is happy with the output).
:- format_predicate(n, dos_newline(_Arg)). dos_newline(default) :- !, dos_newline(1). dos_newline(N) :- ( N > 0 -> write('\r\n'), N2 is N - 1, dos_newline(N2) ; true ).
user, Head is qualified with the definition module of the predicate.