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 predicate bagof/2
. but even with bagof/2
and setof/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
)
generate(X, [Y], [[X| Y]]).
generate(X, S, P) :-
S = [H| T],
append([X], H, Temp),
generate(X, T, Rest),
append([Temp], Rest, P), !.
powerSet([], [[]]).
powerSet(Set, P) :-
Set = [H| T],
powerSet(T, PsT),
generate(H, PsT, Ps),
% write('trying to push '), print(H), write(' to '),
% write('all elements of powerset of '), print(T), write(' which is '),
% print(PsT), nl,
% write('and the result is '), print(Ps), nl, nl,
append(Ps, PsT, P), !.
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.