The format family of predicates is due to Quintus Prolog. They act as a Prolog interface to the C stdio function printf(), allowing formatted output.
Output is formatted according to an output pattern which can have 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 item to print it may be supplied alone. If there are more they have to be given as a list. If there are none then an empty list should be supplied. There has to be as many items as control characters.
The character ~ introduces a control sequence. To print a ~ verbatim just repeat it:
?- format("Hello ~~world!", []).will result in Hello ~world!.
A format may be spread over several lines. The control sequence \c followed by a LFD will translate to the empty string:
?- format("Hello \c world!", []).will result in Hello world!.
Usage:format(Format,Arguments)
Print Arguments onto current output stream according to format Format.
Usage:format(Stream,Format,Arguments)
Print Arguments onto Stream according to format Format.
Usage:sformat(String,Format,Arguments)
Same as Format, Arguments, String)">format_to_string(Format, Arguments, String) (note the different argument order).
Usage:format_to_string(Format,Arguments,String)
Print Arguments onto current string String according to format Format. This predicate is similar to the format/2, but the result is stored in a string.
?- format("Hello~4cworld!", [0'x]).
and
?- format("Hello~*cworld!", [4,0'x]).
both produce
Helloxxxxworld!
The following control sequences are available.
printf("%.Ne", Arg) printf("%.NE", Arg) printf("%.Nf", Arg) printf("%.Ng", Arg) printf("%.NG", Arg)If N is not supplied the action defaults to
printf("%e", Arg) printf("%E", Arg) printf("%f", Arg) printf("%g", Arg) printf("%G", Arg)
?- format("Hello ~1d world!", [42]). ?- format("Hello ~d world!", [42]).
will print as
Hello 4.2 world! Hello 42 world!
respectively.
?- format("Hello ~1D world!", [12345]).
will print as
Hello 1,234.5 world!
?- format("Hello ~2r world!", [15]). ?- format("Hello ~16r world!", [15]).
will print as
Hello 1111 world! Hello f world!
respectively.
?- format("Hello ~16R world!", [15]).
will print as
Hello F world!
?- format("Hello ~4s ~4s!", ["new","world"]). ?- format("Hello ~s world!", ["new"]).
will print as
Hello new worl! Hello new world!
respectively.
?- format("Hello ~i~s world!", ["old","new"]).
will print as
Hello new world!
?- format("Hello ~k world!", [[a,b,c]]).
will print as
Hello .(a,.(b,.(c,[]))) world!
suposing the user has defined the predicate
:- multifile portray/1. portray([X|Y]) :- print(cons(X,Y)).
then
?- format("Hello ~p world!", [[a,b,c]]).
will print as
Hello cons(a,cons(b,cons(c,[]))) world!
?- format("Hello ~q world!", [['A','B']]).
will print as
Hello ['A','B'] world!
?- format("Hello ~w world!", [['A','B']]).
will print as
Hello [A,B] world!
?- format("Hello ~n world!", []).
will print as
Hello world!
The following control sequences are also available for compatibility, but do not perform any useful functions.
Usage:format_control(C)
C is an atom or string describing how the arguments should be formatted. If it is an atom it will be converted into a string with name/2.