format/2 and format/3 act as a Prolog analog to the C stdio function printf(), allowing formatted output 1.5.
Output is formatted according to String which can contain either a format control sequence, or any other character which will appear verbatim in the output. Control sequences act as place-holders for the actual terms that will be output. Thus
?- format("Hello ~q!",world).
will print Hello world!.
If there is only one control sequence, the corresponding element may be supplied alone in Control. If there are more, Control must be a list of these elements. If there are none then Control must be an empty list. There have to be as many elements in Control as control sequences in String.
The character ~ introduces a control sequence. To print
a ~ just repeat it:
?- format("Hello ~~world!", []).
will output Hello ~world!.
The general format of a control sequence is ~NC. The character
C determines the type of the control sequence. N is an
optional numeric argument. An alternative form of N is *. * implies that the next argument in Arguments should
be used as a numeric argument in the control sequence. For example:
?- format("Hello~4cworld!", [0'x]).
and
?- format("Hello~*cworld!", [4,0'x]).
both produce
Helloxxxxworld!
The following control sequences are available in XSB.
~a
The argument is an atom. The atom is printed without quoting.
~Nc
(Print character.) The argument is a number that will be interpreted as an
ASCII code. N defaults to one and is interpreted as the number of
times to print the character.
~f
(Print float). The argument is a float. The float will be printed out by XSB.
~d
(Print integer). The argument is an integer, and will be printed out by XSB.
~Ns
(Print string.) The argument is a list of ASCII codes. Exactly N characters will be printed. N defaults to the length of the
string. Example:
?- format("Hello ~4s ~4s!", ["new","world"]).
?- format("Hello ~s world!", ["new"]).
will print as
Hello new worl! Hello new world!
respectively.
~i
(Ignore argument.) The argument may be of any type. The argument will be
ignored. Example:
?- format("Hello ~i~s world!", ["old","new"]).
will print as
Hello new world!
~k
(Print canonical.) The argument may be of any type. The argument will be
passed to write_canonical/2 ). Example:
?- format("Hello ~k world!", a+b+c).
will print as
Hello +(+(a,b),c) world!
~q
(Print quoted.) The argument may be of any type. The argument will be
passed to writeq/2. Example:
?- format("Hello ~q world!", [['A','B']]).
will print as
Hello ['A','B'] world!
~w
(write.) The argument may be of any type. The argument will be passed
to write/2. Example:
?- format("Hello ~w world!", [['A','B']]).
will print as
Hello [A,B] world!
~Nn
(Print newline.) Print N newlines. N defaults to 1.
Example:
?- format("Hello ~n world!", []).
will print as
Hello world!