Union and intersection

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

pl/prolog/pllib/union_and_intersection.txt · ostatnio zmienione: 2019/06/27 15:50 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0