Gathering all solutions without findall

2019-01-29 13:13发布

问题:

So as part of my work my code needs to print all the solutions to a query but without using the findall/3 predicate. I've done some reading around and there are ways involving adding the solutions to a list and so on. I tried doing this myself but to with no success; hence I was hoping someone may be able to show how I would print all the solutions without using findall.

The program code is as follows:

    solutions(Q, 100):-
        Q = [X, Y, S],
        between(2,50,X),
        between(2,50,Y),
        S is X+Y,
        Y > X,
        S =< 50.

The Q and the 100 are there because it's needed for another part of the program so ignore that for now. When I query using ?- solutions(Q, 100) I get the results [2,3,5], [2,4,6], [2,5,7] and so on but obviously I need to press ; to get each new result. I need all these to be displayed without the need to press ; and without using findall.

回答1:

An assert-based solution (which is actually how findall was implemented in Prolog textbooks): Assume that solution/2 finds each single solution, as per your code. Now we use a fail-driven loop, as Paulo suggested, to build a list of solutions, using assert/1 to cache solutions.

solutions(_, N) :-
  solution(Q, N),
  (cache(Qs) -> retractall(cache(_)) ; Qs = []),
  assert(cache([Q|Qs])),
  fail.
solutions(Qs, _) :-
  retract(cache(Qs)).


回答2:

You can use a failure-driven loop. Try:

?- solutions(Q, 100), write(Q), nl, fail.


标签: prolog