我应该在Prolog中编写一个程序,当给出一个列表时,返回它的powerset的排列。
我忘了提一件事:我已经有一个反转列表的谓词:deep_reverse(List,RevList)。
例如:?-sublist_perm([a,b,c],X).
将返回:(允许重复)
X = [] ;
X = [c] ;
X = [b] ;
X = [b, c] ;
X = [c, b] ;
X = [a] ;
X = [a, c] ;
X = [c, a] ;
X = [a, b] ;
X = [b, a] ;
X = [a, b, c] ;
X = [b, a, c] ;
X = [b, c, a] ;
X = [a, c, b] ;
X = [c, a, b] ;
X = [c, b, a]
答案 0 :(得分:4)
您在一个问题中提出两件事:如何获取所有子列表以及如何排列列表:
sublist_perm(In, Out) :-
sublist(In, Temp),
permutation(Temp, Out).
sublist([], []).
sublist([_|XS], YS) :-
sublist(XS, YS).
sublist([X|XS], [X|YS]) :-
sublist(XS, YS).
另请参阅:man page for permutation/2。
findall(X, sublist_perm([a,b,c], X), XS),
XS = [[],[c],[b],[b,c],[c,b],[a],[a,c],[c,a],[a,b],[b,a],
[a,b,c],[b,a,c],[b,c,a],[a,c,b],[c,a,b],[c,b,a]].