如何获取包含另一个列表中元素的所有可能组合的列表?
How to get a list with all the possible combinations of the elements inside another list?
我有一个包含 as 元素的列表:原子和其他列表。我问以下问题:
-? totobola([1,x,2,1,x,2,1,x,2,1,x,[1,x],[1,x,2]],LS).
我希望 LS
成为一个列表,我会在其中显示每个原子,后跟给定列表中列表的可能组合。
LS=[1,x,2,1,x,2,1,x,2,1,x,1,1];
LS=[1,x,2,1,x,2,1,x,2,1,x,1,x];
LS=[1,x,2,1,x,2,1,x,2,1,x,1,2];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,1];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,x];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,2];
no
我目前的解决方案:
lista([_|_]):- true, !.
lista(_):- false.
totobola([],[]).
totobola([X|T1],[Y|T2]):-
lista(X),
!,
member(Y,X),
totobola(T1,T2).
totobola([X|T1],[X|T2]):-
totobola(T1,T2).
使用 lista
我检查 X
是否是一个列表但不是得到 LS=[1,x,2,1,x,2,1,x,2,1,x,1,1];
我得到 LS = [1, x, 2, 1, x, 2, 1, x, 2|...]
有人可以指导我,或者至少告诉我我做错了什么或做错了什么吗?
提前致谢!
进行 'repeat.' 查询,然后按 'w' 键和“.”您将进入写入模式并查看列表中的所有值。
?- 重复。
true [write] --> 按 w 一次后
真的 。 --> 输入一个点退出
我们在 ALGAV class,ISEP(葡萄牙)中进行了此练习,解决方案是:
is_list(X) :- var(X), !, fail.
is_list([]).
is_list([_|T]) :- is_list(T).
totobola([],[]).
totobola([H|T],[X|LR]):- is_list(H), !, member(X,H), totobola(T,LR).
totobola([H|T],[H|LR]):- totobola(T,LR).
我有一个包含 as 元素的列表:原子和其他列表。我问以下问题:
-? totobola([1,x,2,1,x,2,1,x,2,1,x,[1,x],[1,x,2]],LS).
我希望 LS
成为一个列表,我会在其中显示每个原子,后跟给定列表中列表的可能组合。
LS=[1,x,2,1,x,2,1,x,2,1,x,1,1];
LS=[1,x,2,1,x,2,1,x,2,1,x,1,x];
LS=[1,x,2,1,x,2,1,x,2,1,x,1,2];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,1];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,x];
LS=[1,x,2,1,x,2,1,x,2,1,x,x,2];
no
我目前的解决方案:
lista([_|_]):- true, !.
lista(_):- false.
totobola([],[]).
totobola([X|T1],[Y|T2]):-
lista(X),
!,
member(Y,X),
totobola(T1,T2).
totobola([X|T1],[X|T2]):-
totobola(T1,T2).
使用 lista
我检查 X
是否是一个列表但不是得到 LS=[1,x,2,1,x,2,1,x,2,1,x,1,1];
我得到 LS = [1, x, 2, 1, x, 2, 1, x, 2|...]
有人可以指导我,或者至少告诉我我做错了什么或做错了什么吗?
提前致谢!
进行 'repeat.' 查询,然后按 'w' 键和“.”您将进入写入模式并查看列表中的所有值。
?- 重复。
true [write] --> 按 w 一次后
真的 。 --> 输入一个点退出
我们在 ALGAV class,ISEP(葡萄牙)中进行了此练习,解决方案是:
is_list(X) :- var(X), !, fail.
is_list([]).
is_list([_|T]) :- is_list(T).
totobola([],[]).
totobola([H|T],[X|LR]):- is_list(H), !, member(X,H), totobola(T,LR).
totobola([H|T],[H|LR]):- totobola(T,LR).