Reputation: 5405
I'm Following Prolog Tutorial 2.1.
Program
adjacent(1, 2).
adjacent(1, 3).
adjacent(1, 4).
main:-
adjacent(1, R),
write(R).
prints 2
.
But it supposes to print a list of possible values according to the tutorial:
?- adjacent(1,2).
yes
?- adjacent(1,3).
no
?- adjacent(1,R).
R = 2 ;
R = 3 ;
R = 4 ;
no
I try again in repl only to get the same result:
?- adjacent(1, R).
R = 2 .
How could I get/print a list of possible values of a variable?
Upvotes: 2
Views: 4336
Reputation: 1
after Prolog prints R = 2;
, you can press "r", "n", [TAB] or [SPACE] to show the next results.
I don't know how that works with write(R).
but that is not in the code from the tutorial so I think that's supposed to be the trick
Upvotes: 0
Reputation: 1
You need a failure-loop:
adjacent(1, 2).
adjacent(1, 3).
adjacent(1, 4).
main :-
adjacent(1, R),
write(R), nl,
fail.
main.
This is a basic programming technique in Prolog. fail/0 will force backtracking, so next adjacent/2 solution is explored (and so on). Second clause to main/0 prevents the loop itself from failure.
Upvotes: 0
Reputation: 60014
In swipl, library(apply) is - by default - autoloaded, so you can write
main:- forall(adjacent(1, R), (write(R),nl)).
note: Action is a conjuction, just for to illustrate the proper syntax required. For any practical purpose, main :- forall(adjacent(1, R), writeln(R)).
could be better.
Upvotes: 1