swi-prologprolog-toplevel

How to see all of the answers in SWI-Prolog without pressing spacebar?


Simple example:

?- between(1,10,X).
X = 1 ;
X = 2 ;
X = 3 ;
X = 4 ;
X = 5 ;
X = 6 ;
X = 7 ;
X = 8 ;
X = 9 ;
X = 10.

When this is done using SWI-Prolog using the REPL to see the next answer the spacebar has to be pressed.

How can all of the results be listed to the screen without pressing the spacebar?


Note on similar question.

If you arrived at this question by a search and your real problem is

I'm using SWI-Prolog and I'm trying to print a list but if the list has more than 9 items - it look like that -

[1, 15, 8, 22, 5, 19, 12, 25, 3|...] 

is there a way to show the whole list?

Then see these Q&A:

SWI-Prolog - show long list
SWI-Prolog how to show entire answer (list)?


Solution

  • A "hackish" solution is to add print(X), nl, fail to the call. Here print(X) can of course print any relevant information. For example between(1,10,X), print(X), nl, fail.

    This works since print/1 [swi-doc] is just another predicate that prints the term that is passed to it. nl/0 [swi-doc] will print a new line character, and fail/0 [swi-doc] always fails.

    We thus let Prolog to propose solutions, print these, print a new line, and the fail will "activate" the backtracking mechanism that will aim to look for another solution that is again printed and fails.

    Eventually all solutions are printed, and thus then the call fails. This thus yields:

    ?- between(1,10,X), print(X), nl, fail.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    false.