Getting the nth query solution

Can I extract the nth query solution? I know findall, but I think (correct me if I am wrong) that it creates a whole list of solutions, and I would prefer an approach that consumes as much memory as necessary to calculate the value.

+4
source share
1 answer

Here is a simple way by which SWI-Prolog allows you to calculate exactly N solutions (where you can specify the number N yourself).

In this example, N is 17, and the goal, the 17th solution of which we are interested in, is between(0, inf, I):

?- findnsols(17, I, between(0, inf, I), L), last(L, X), !.
L = [0, 1, 2, 3, 4, 5, 6, 7, 8|...],
X = 16.

, . findnsols/4 (.. 17 ).

+1

All Articles