开发者

Putting all results of a query in a list in Prolog

开发者 https://www.devze.com 2023-01-28 06:53 出处:网络
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.

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 s开发者_Python百科ay 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.


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).
0

精彩评论

暂无评论...
验证码 换一张
取 消