I wish to define a predicate powerset(X, P) which is true when P is the powerset of X. Should work whether or not P is ground.
Asked
Active
Viewed 6,089 times
1
-
what have you tried so far? Which prolog are you working on? Is this homework? – Chetter Hummin Apr 26 '12 at 17:48
-
STICStus. This is a practice question I cannot work out a solution to. – user1283759 Apr 26 '12 at 17:49
-
What code do you have so far? – Chetter Hummin Apr 26 '12 at 17:51
-
@gusbro: No, it's the difference between subsequence and substring. – false Jan 31 '13 at 19:33
-
@false: you are right. comment deleted (will delete this in a while) – gusbro Jan 31 '13 at 19:57
-
@gusbro: Just noted this, because someone wants to close this question... – false Jan 31 '13 at 20:04
1 Answers
6
Since you use SICStus Prolog you can use the subseq0(+Sequence, ?SubSequence) from library(lists), which "is true when SubSequence is a subsequence of Sequence, but may be Sequence itself" (Quoting from the manual http://www.sics.se/sicstus/docs/4.0.2/html/sicstus/lib_002dlists.html).
?- setof(X, subseq0([a,b,c],X), Xs).
Xs = [[],[a],[a,b],[a,b,c],[a,c],[b],[b,c],[c]]
If you are not allowed to use library predicates you can implement the subseteq0 as explained in gnu Prolog powerset modification, which I quote here for the sake of completeness (with thanks to gusbro)
powerset([], []).
powerset([H|T], P) :- powerset(T,P).
powerset([H|T], [H|P]) :- powerset(T,P).

Community
- 1
- 1

Alexander Serebrenik
- 3,567
- 2
- 16
- 32
-
1I just noted [this odd terminological problem](http://en.wikipedia.org/wiki/Subsequence#Substring_vs._subsequence): A sequence and a string are often used synonymously, but a subsequence and a substring are not identical... – false Apr 26 '12 at 21:39
-
1Somehow I'm used to this naming convention but indeed it is not very consistent. – Alexander Serebrenik Apr 27 '12 at 08:49