swi-prolog print multiple variables

自作多情 提交于 2021-01-28 22:20:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!