Set Operations

check_determ
domains
  int  = integer
  ints = int*
predicates
  nondeterm member(int,ints)
  difference(ints,ints,ints)
  intersection(ints,ints,ints)
  union(ints,ints,ints)
clauses

/* member(X, Xs) is true if the element X is contained in the list Xs.     */
member(X, [X|_]).
member(X, [_|Xs]):-member(X, Xs).

/* difference(Xs, Ys, Zs) is true if Zs is the list of the set of elements */
/*   in the difference of the sets of elements in lists Xs and Ys.         */
difference([], _, []).
difference([X|Xs], Ys, Zs):-member(X, Ys), !, difference(Xs, Ys, Zs).
difference([X|Xs], Ys, [X|Zs]):-difference(Xs, Ys, Zs).
 
/* intersection(Xs, Ys, Zs) is true if Zs is the list of the set of        */
/*   elements in the intersection of the sets of elements in lists Xs and  */
/*   Ys.                                                                   */
intersection([], _, []).
intersection([X|Xs], Ys, [X|Zs]):-
  member(X, Ys), !, intersection(Xs, Ys, Zs).
intersection([_|Xs], Ys, Zs):-intersection(Xs, Ys, Zs).
 
/* union(Xs, Ys, Zs) is true if Zs is the list of the set of elements in   */
/*   the union of the sets of elements in lists Xs and Ys.                 */
union([], Ys, Ys).
union([X|Xs], Ys, Zs):-member(X, Ys), !, union(Xs, Ys, Zs).
union([X|Xs], Ys, [X|Zs]):-union(Xs, Ys, Zs).

Turbo Prolog Index     Home Page