So i got this for powerset:
powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).
This generates all sets of a list. Is it possible to generate all sets in list order.
Example:
List = [a,b,c]
I want to get
[a],[a,b],[a,b,c],[b],[b,c],[c]
Note there is no [a,c]
in this list of subsets since these are subsets starting from the left and going to the right.
I've tried using a combination of append and recursion, but that didn't work out as i wanted it to. Little stumped at this point.
Thanks.
How about
You want all
subsequencessubstrings (definition). Grammars (DCGs) are best for this:(SWI-Prolog)
How about this:
I know this an old post, but I'm not gonna update it for no reasons. the answer which is accepted with all the respect, generates all subsets separably, and does not generate a powerset.
a few days ago I was trying to implement a
powerSet/2
predicate, without use of built-in predicatebagof/2
. but even withbagof/2
andsetof/2
it's not very easy problem for beginners (generating all subset separably is another problem and much easier). so after implementing the solution I thought it's better to put it here in order to prevent people who are searching for this topic from mistakes.My solution (without
bagof/2
)code will be understood if consulted with the commented lines.
another solution is available here which uses built-in predicate
bagof/3
.probably it would be more helpful now.