Try the following two programming exercises:
- Sets can be thought of as lists that don’t contain any repeated elements. For example,
is a set, but
is not (as it contains two occurrences of
. Write a Prolog program
that is satisfied when the first argument is a subset of the second argument (that is, when every element of the first argument is a member of the second argument). For example:
Your program should be capable of generating all subsets of an input set by backtracking. For example, if you give it as input
it should successively generate all eight subsets of [a,b,c] .
- Using the
predicate you have just written, and
, write a predicate
that takes a set as its first argument, and returns the powerset of this set as the second argument. (The powerset of a set is the set of all its subsets.) For example:
It doesn’t matter if the sets are returned in some other order. For example,
is fine too.