I’m a beginner in SWI-Prolog and I am trying to get multiple solutions from a query.
test(1, 1). test(1, 2). test(2, 1). test(2, 2). ?- test(X, Y). X = Y, Y = 1 ; X = 1, Y = 2 ; X = 2, Y = 1 ; X = Y, Y = 2.
However, I have to press semicolon to get the next solution and it’s inconvenient. I searched the Internet and found an approach. It seems that
findnsols could help.
I blindly tried the approach to get at most 2 solutions, it works.
?- findnsols(2, [X, Y], test(X, Y), Xs), !. Xs = [[1, 1], [1, 2]].
So far so good!
Then I tried to read the document of
findnsols from SWI-Prolog -- Manual , but it make me some confusion.
findnsols (+N, @Template, :Goal, -List)
My questions are
Is my approach above correct? i.e. using
cutat the end of query to get at most N solutions.
What exactly Template is?
What’s the meaning of
@at the predicate declares in the document?