简体   繁体   English

Prolog:如何更改组合(N,[H | T],P)的输出以返回对列表,而不是;之前的第一个?

[英]Prolog: how can I change the output of combinations(N, [H|T], P) to return a list of pairs, rather than just the first one before ;?

Prolog: How can I change the output of combinations(N, [H|T], P) to return a list of pairs, rather than just the first one before ; Prolog:如何更改组合(N,[H | T],P)的输出以返回对列表,而不是仅返回第一个对; ? The program works well as long as I press ; 只要我按下,该程序就会运作良好; in the command line, but I want to return directly a list of pairs. 在命令行中,但我想直接返回对列表。

comb(1, [H|_], [H]).
comb(N, [H|T], [H|C]) :- N1 is N - 1, N1 > 0, comb(N1, T, C).
comb(N, [_|T], C):- comb(N, T, C).

This is my program. 这是我的程序。 Thank you very much! 非常感谢你!

You are looking for findall/3 . 您正在寻找findall/3

findall(+Template, :Goal, -Bag)

Create a list of the instantiations Template gets successively on backtracking over Goal and unify the result with Bag . 创建一个实例化列表。 Template依次在Goal上回溯,并与Bag统一结果。 Succeeds with an empty list if Goal has no solutions. 如果Goal没有解决方案,则列表为空。 findall/3 is equivalent to bagof/3 with all free variables bound with the existential operator ( ^ ), except that bagof/3 fails when Goal has no solutions. findall/3等效于bagof/3 ,所有自由变量都与存在运算符( ^ )绑定,但在Goal没有解决方案时bagof/3失败。

Example: 例:

?- findall(X, comb(2, [a,b,c,d], X), Xs).
Xs = [[a, b], [a, c], [a, d], [b, c], [b, d], [c, d]].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM