How to block/hide variables in output.

230 views
Skip to first unread message

Łukasz Nizioł

unread,
Jun 7, 2018, 5:00:18 AM6/7/18
to SWI-Prolog
Hi all,

I have problem with formating output.

For example, i have this:

a(A,B):- A is B.

Input:

?- a(W,1),
write("My number is "),
write(W).

Output:

My number is 1
W = 1

Is there any way to hide W =1, and have output like this:

My number is 1

 

Łukasz Nizioł

unread,
Jun 7, 2018, 5:19:02 AM6/7/18
to SWI-Prolog
I have found solution: 

?- a(_W,1),

write("My number is "),
write(_W)

but now I have output:

My number is 1
1true

Jan Wielemaker

unread,
Jun 7, 2018, 5:30:23 AM6/7/18
to Łukasz Nizioł, SWI-Prolog
There are two `worlds'. If you use Prolog interactively you typically
do not use read/write, but just pose queries and get answers from the
toplevel loop. Thatway you can easily combine queries, use their output
for other tasks in Prolog, etc.

If you use it as an application, you typically either provide the query
on the commandline or add it to the application using
initialization/1,2. E.g., you can do (note that format/2,3 is a much
nicer way to write output for users than lots of write calls).

% swipl -g "a(W,1),format('My number is ~w~n',[W])" -t halt myprog.pl

Or you can add this to your file:

:- initialization(go, main).

go :-
a(_W,1),
format('My number is ~w~n',[W]).

and run

% swipl myprog.pl

or add a first line as below, make the file executable (chmod +x myprog.pl)
and simply run as ./myprog.pl

#!/usr/bin/env swipl

or, finally

% swipl -o myprog -c myprog.pl
% ./myprog

This all assumes a fairly recent version of SWI-Prolog and a non-windows
system. On Windows you can do similar things, but it is all a bit
different.

Cheers --- Jan
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages