Introduction
The predicate format
in SWI-Prolog produces formatted output in console, however it is not easy to use. The examples given below try to provide some typical applications of the predicate. Further usages of the predicate can be found on the official site of SWI-Prolog.
A predefined function with values filling the output
f1(X,Y) :- member(X,[3,7,5,11,104]), Y is X^2.
Trial 1
try1 :- format('Only substitution~n'), format('~`-t ~20|~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('The square of ~w : ~w~n',T)), nl.
Only substitution ------------------- The square of 3 : 9 The square of 7 : 49 The square of 5 : 25 The square of 11 : 121 The square of 104 : 10816
Trial 2
try2 :- format('Left aligned~n'), format('~`-t ~20|~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('~w ~8| ~w~n',T)), nl, findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('~w ~`.t~8| ~w~n',T)), nl.
Left aligned ------------------- 3 9 7 49 5 25 11 121 104 10816 3 ...... 9 7 ...... 49 5 ...... 25 11 ..... 121 104 .... 10816
Trial 3
try3 :- format('Right aligned~n'), format('~`-t ~20|~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('~w ~t ~w~20|~n',T)), nl, findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('~w ~`.t ~w~20|~n',T)), nl.
Right aligned ------------------- 3 9 7 49 5 25 11 121 104 10816 3 ................ 9 7 ............... 49 5 ............... 25 11 ............. 121 104 .......... 10816
Trial 4
try4 :- format('Center aligned~n'), format('~`-t ~20|~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('~t [~w^2]=~w ~t~20|~n',T)), nl.
Center aligned ------------------- [3^2]=9 [7^2]=49 [5^2]=25 [11^2]=121 [104^2]=10816
Trial 5
try5 :- format('Table (1 column)~n'), format('|~`-t~20||~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('|~w^2=~w ~t~20||~n',T)), format('|~`-t~20||~n'), nl, format('Table (2 columns)~n'), format('Without inner border~n'), format('|~`-t~20||~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('|~w^2~8|~w ~t~20||~n',T)), format('|~`-t~20||~n'), nl, format('Table (2 columns)~n'), format('Left aligned~n'), format('|~`-t~8||~`-t~20||~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('|~w^2~8|| ~w ~t~20||~n',T)), format('|~`-t~8||~`-t~20||~n'), nl, format('Table (2 columns)~n'), format('Right aligned~n'), format('|~`-t~8||~`-t~20||~n'), findall([X,Y],f1(X,Y),Tuples), forall(member(T,Tuples), format('|~w^2~8||~t~w~20||~n',T)), format('|~`-t~8||~`-t~20||~n'), nl.
Table (1 column) |-------------------| |3^2=9 | |7^2=49 | |5^2=25 | |11^2=121 | |104^2=10816 | |-------------------| Table (2 columns) Without inner border |-------------------| |3^2 9 | |7^2 49 | |5^2 25 | |11^2 121 | |104^2 10816 | |-------------------| Table (2 columns) Left aligned |-------|-----------| |3^2 | 9 | |7^2 | 49 | |5^2 | 25 | |11^2 | 121 | |104^2 | 10816 | |-------|-----------| Table (2 columns) Right aligned |-------|-----------| |3^2 | 9| |7^2 | 49| |5^2 | 25| |11^2 | 121| |104^2 | 10816| |-------|-----------|
No comments:
Post a Comment