多个列表的组合 - Prolog

Liu*_*nic 4 list prolog

我需要在列表列表中找到组合。例如,给出以下列表,

List = [[1, 2], [1, 2, 3]]
Run Code Online (Sandbox Code Playgroud)

这些应该是输出,

Comb = [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3]]
Run Code Online (Sandbox Code Playgroud)

另一个例子:

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

Comb = [[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3]....etc]
Run Code Online (Sandbox Code Playgroud)

我知道如何为具有两个子列表的列表执行此操作,但它需要适用于任意数量的子列表。

我是 Prolog 的新手,请帮忙。

rep*_*eat 5

这个答案寻找“一个纯粹的解决方案,也考虑到Ess”提供的赏金。在这里,我们将之前的答案概括如下:

list_crossproduct(Xs, []) :-
   member([], Xs).
list_crossproduct(Xs, Ess) :-
   Ess = [E0|_],
   same_length(E0, Xs),
   maplist(maybelonger_than(Ess), Xs),
   list_comb(Xs, Ess).

maybelonger_than(Xs, Ys) :-
   maybeshorter_than(Ys, Xs).

maybeshorter_than([], _).
maybeshorter_than([_|Xs], [_|Ys]) :-
   maybeshorter_than(Xs, Ys).
Run Code Online (Sandbox Code Playgroud)

list_crossproduct/2通过关联XsEss早期获得双向。

?- list_comb(Xs, [[1,2,3],[1,2,4],[1,2,5]]).
非终止                                % 不好!

?- list_crossproduct(Xs, [[1,2,3],[1,2,4],[1,2,5]])。
   Xs = [[1],[2],[3,4,5]] % 现在也可以使用了
; 错误的。

具有多个答案的示例查询:

?- list_crossproduct(Xs, [[1,2,3],[1,2,4],[1,2,5],X,Y,Z]).
   X = [1,2,_A],
   Y = [1,2,_B],
   Z = [1,2,_C], Xs = [[1],[2],[3,4,5,_A,_B,_C]]
;  X = [1,_A,3],
   Y = [1,_A,4],
   Z = [1,_A,5], Xs = [[1],[2,_A],[3,4,5]]
;  X = [_A,2,3],
   Y = [_A,2,4],
   Z = [_A,2,5], Xs = [[1,_A],[2],[3,4,5]]
;  false.
Run Code Online (Sandbox Code Playgroud)