问题
I want to print multiple variables in swi-prolog, currently I am writing in this way:
writeln('child link : '),
writeln(LINK_CHILD),
writeln('rule - four'),
writeln(REND_PARENTI),
writeln(REND_CHILDI)
so every variable gets printed on new line, I could not figure out way to print them in single line. I appreciate any help
回答1:
You can either use write
instead of writeln
, or even better, use format:
?- format("~a~n~a:~a~n", [x, y, z]).
x
y:z
true.
Or, for your case, something like:
format("child link: ~w rule - four ~w", [LINK_CHILD, REND_PARENT....]),
flush_output
You need the flush_output
if you want to print out before you get to the end of the line. See SWI-Prolog's primitive character IO.
回答2:
Writing on a single line would be done with write/1
i.o. writeln/1
.
Writing an arbitrary number of atoms can be done by calling a predicate (write/1
in this case) for a list of arguments (the list of printed atoms in this case) by using maplist/3
.
The resultant code is:
?- maplist(write, ['child link : ',LINK_CHILD,'rule - four',REND_PARENTI,REND_CHILDI]).
来源:https://stackoverflow.com/questions/29230608/swi-prolog-print-multiple-variables