问题
I'd like to know how to make a predicate that puts all results obtained from some query (so I get a result and press semicolon until I get False) in a list.
For example if I write foo(X,[1,2,3]).
in some Prolog listener, let's say the result is
X=[11];
X=[22];
False.
I would like to get all those results in a list, so something like the following would happen.
?-another_foo(X,[1,2,3]).
X=[[11],[22]].
another_foo would somehow use foo to create a list with all the results from foo. I just don't know how.
回答1:
Use the built-in predicate findall/3
:
?-findall(X0, foo(X0, [1,2,3]), X).
X = [[11], [22]].
You can define your another_foo/2
:
another_foo(X, Input) :-
findall(X0, foo(X0, Input), X).
来源:https://stackoverflow.com/questions/4340022/putting-all-results-of-a-query-in-a-list-in-prolog