====== Union and intersection ====== {{tag>math recursion}} ===== Description ===== Finding the union and intersection of two lists **Source**: The Art of Prolog ===== Download ===== Program source code: {{union_and_intersection.pl}} ===== Listing ===== /* union_intersect(Xs,Ys,Us,Is) :- Us and Is are the union and intersection, respectively, of the elements in Xs and Ys. */ union_intersect([X|Xs],Ys,Us,[X|Is]) :- member(X,Ys), union_intersect(Xs,Ys,Us,Is). union_intersect([X|Xs],Ys,[X|Us],Is) :- nonmember(X,Ys), union_intersect(Xs,Ys,Us,Is). union_intersect([],Ys,Ys,[]). % Program 13.3 Finding the union and intersection of two lists ===== Comments =====