[英]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 overGoal
and unify the result withBag
.创建一个实例化列表。
Template
依次在Goal
上回溯,并与Bag
统一结果。 Succeeds with an empty list ifGoal
has no solutions.如果
Goal
没有解决方案,则列表为空。findall/3
is equivalent tobagof/3
with all free variables bound with the existential operator (^
), except thatbagof/3
fails whenGoal
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.