Prolog Loops with Variable Results

I have a small program written in Prolog. At the moment, I can print the first result with

test(X, 1, 4, 5).
write(X).

But if there is more than one result for X, how to print the following?

Thanks.

+5
source share
3 answers

Use an error loop:

test(X, 1, 4, 5), writeln(X), fail ; true.

or the same in a more readable way using forall/2:

forall(test(X, 1, 4, 5), writeln(X)).

There is no need to create a list of all solutions (used for this findall/3) if you do not need this list for anything else but print it.

If your Prolog does not forall/2, do it yourself as follows:

forall(A, B) :-
    \+ (call(A), \+ call(B)).
+7
source

? backtrack; . , . , .

: ;. "" , .

   printList([]) :- write("").
   printList([H|T]) :- write(H), printList(T).
+2

';' findall :

findall(X,test(X,1,4,5),L).

, , - , , X .

, L, , . , L , , .

+2
source

All Articles