I am trying to create code that generates all subsets of a set in order . That is, a call to subset([1,2,3], X) should generate
X = []; X = [1]; X = [2]; X = [3]; X = [1,2]; X = [1,3]; X = [2,3]; X = [1,2,3].
The internal order is not so important, only the first smallest subsets are listed first (that is, I do not care that [2,3] precedes [1,2], only 1 , [2] and [3] to [2,3] )
-
I have tried two approaches so far. At first I tried to predicate myself ...
subset([], []). subset(List, []). subset(List, [N]) :- member(N, List). subset(List, [N|Rest]) :- !, nth0(I, List, N), findall(E, (nth0(J, List, E), J > I), NewList), subset2(NewList, Rest).
... but it is not even suitable for its intended purpose. Secondly, I tried to make poweret (using this subset predicate ) and sort using list_to_ord_set / 2, but I couldn’t get it working.
reference