Prolog - 查找列表清单(列表)的所有组合(产品)

时间:2017-06-23 23:00:32

标签: prolog combinations prolog-findall

我尝试了一些功能来实现一个查找所有组合的谓词,例如:

List = [[1, 2], [1, 2, 3]]

这些应该是输出,

Comb = [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3]] 

但我发现的所有解决方案都使用findall,我不想在我的任务中使用它。

如何以不同的方式实现谓词,避免使用findall

或者如何在不使用任何内置功能的情况下实现my_findall

A solution like here, without built-in predicates would be great

感谢帮助者!

2 个答案:

答案 0 :(得分:5)

我不确定这是最有效的方法,但它相当透明。这里的想法是在递归(或归纳)"层中定义问题":

% multiply_lists(ListOfLists, MultipliedListOfLists)
%
% The first two clauses handle the case where ListOfLists consists
%   of just one list
% The third clause handles the general case
%
multiply_lists([[X]], [[X]]).
multiply_lists([[X|Xs]], [[X]|T]) :- 
    multiply_lists([Xs], T).
multiply_lists([E|Es], R) :-
    multiply_lists(Es, R1),
    multiply_list(E, R1, R).

% multiply_list relates the product of a list of lists and a single list
%    of elements
%
multiply_list([], _, []).
multiply_list([E|Es], L, Ls) :-
    multiply_list(Es, L, LL),
    multiply_element(E, L, LL, Ls).

% multiply_element relates the product, prepended to a given list,
%   of a single list of lists and a single element 
%
multiply_element(_, [], A, A).
multiply_element(X, [Y|Ys], A, [[X|Y]|T]) :-
    multiply_element(X, Ys, A, T).

multiply_element/4实际上将两个规则合并为一个:它将列表的乘法定义为单个元素,并将这些结果作为单个元素添加到给定列表中。

示例结果:

| ?- multiply_lists([[1, 2], [1, 2, 3]], L).

L = [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3]] ? ;

no
| ?- multiply_lists([[a,b,c], [1,2], [x,y]], L).

L = [[a,1,x],[a,1,y],[a,2,x],[a,2,y],[b,1,x],[b,1,y],[b,2,x],[b,2,y],[c,1,x],[c,1,y],[c,2,x],[c,2,y]] ? ;

no

上述实施的一些怪癖:

  • 它不是尾递归(所以当列表变长时会使用更多堆栈)
  • 它留下了一个选择点

但它确实说明了如何在不使用append/3或其他基于列表的预定义谓词的情况下解决问题。

答案 1 :(得分:0)

您可以使用“The Prraft of Colog”中的findall实现。但无论如何,您最终都会使用内置的元谓词。

关于你的linked solution

try([],[]).
try([L|Ls],[M|Ms]):-
  member(M,L),
  try(Ls,Ms).

findall2( Template, Enumerator, List ) :-
  asserta( 'find all'( [] ) ),
  call( Enumerator ),
  asserta( 'find all'( {Template} ) ),
  fail
;
  all_found( [], List ).


all_found( SoFar, List ) :-
  retract('find all'( Item ) ),
  !,
  /*  to stop retract looking for more Items.  */
  all_found( Item, SoFar, List ).


all_found( [], List, List ).

all_found( {Template}, SoFar, List ) :-
  all_found( [Template|SoFar], List ).

所以你会得到

?- List = [[1, 2], [1, 2, 3]], findall2(M,try(List,M),Comb).
List = [[1, 2], [1, 2, 3]],
Comb = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]].
相关问题