Power set: Difference between revisions

Content deleted Content added
Peak (talk | contribs)
m →‎Logical (cut-free) Definition: remove redundant comment
Peak (talk | contribs)
→‎Logical (cut-free) Definition: clarify meaning of efficiency
Line 1,712: Line 1,712:
The predicate subseq(X,Y) is true if and only if the list X is a subsequence of the list Y.
The predicate subseq(X,Y) is true if and only if the list X is a subsequence of the list Y.


The specifications here are elementary, logical (cut-free), and efficient.
The definitions here are elementary, logical (cut-free), and efficient (within the class of comparably generic implementations).
<lang Prolog>
<lang Prolog>powerset(X,Y) :- bagof( S, subseq(S,X), Y).
powerset(X,Y) :- bagof( S, subseq(S,X), Y).


subseq( [], []).
subseq( [], []).
Line 1,722: Line 1,721:
</lang>
</lang>
Output :
Output :
<pre> ?- powerset([1,2,3], X).
<pre>
?- powerset([1,2,3], X).
X = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]].
X = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]].
</pre>
</pre>