
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
Prolog - Intersection of Set
In Prolog, we generally represents Sets using List. In this chapter, we're discussing how to get Intersection of two sets by defining a predicate.
Getting Intersection of Sets represented by a List
Let us define a clause called list_intersection(L1,L2,L3), So this will take L1 and L2, and perform Intersection operation, and store the result into L3. Intersection will return those elements that are present in both lists. So L1 = [a,b,c,d,e], L2 = [a,e,i,o,u], then L3 = [a,e]. Here, we will use the list_member() clause to check if one element is present in a list or not.
Program (list_set.pl)
list_member(X,[X|_]). list_member(X,[_|TAIL]) :- list_member(X,TAIL). list_intersect([X|Y],Z,[X|W]) :- list_member(X,Z), list_intersect(Y,Z,W). list_intersect([X|Y],Z,W) :- \+ list_member(X,Z), list_intersect(Y,Z,W). list_intersect([],Z,[]).
Output
| ?- consult('D:/TP Prolog/Sample Codes/list_set.pl'). compiling D:/TP Prolog/Sample Codes/list_set.pl for byte code... D:/TP Prolog/Sample Codes/list_set.pl:5: warning: singleton variables [Z] for list_intersect/3 D:/TP Prolog/Sample Codes/list_set.pl compiled, 4 lines read - 1510 bytes written, 4 ms yes | ?- list_intersect([a,b,c,d,e],[a,e,i,o,u],L3). L3 = [a,e] ? yes | ?- list_intersect([a,b,c,d,e],[],L3). L3 = [] yes | ?-
Explanation
list_member(X,[X|_]) represents the base case checking a member where element is head of the list.
-
list_member(X,[_|TAIL]) :- list_member(X,TAIL) represents the case checking a member where element is in the tail of the list.
list_member(X,[_|TAIL]) is to check X in the tail of the list.
list_member(X,TAIL) is a recursive call checking X in the tail of the List.
-
list_intersect([X|Y],Z,[X|W]) :- list_member(X,Z), list_intersect(Y,Z,W). returns W as a intersection of first two Sets where X is head and is common element of both sets.
list_intersect([X|Y],Z,[X|W]) is to divide the first list where X represents the head of the list and Y represents the tail of the list. Z is second Set. X|W is intersection of sets.
list_member(X,Z) checks the X to be member of Z. If X is member of Z then Intersection is calculated on Y and Z excluding X as it will be added as head to the intersection set.
-
list_intersect([X|Y],Z,W) :- \+ list_member(X,Z), list_intersect(Y,Z,W). returns W as a intersection of first two Sets where X is not a common head element.
list_intersect([X|Y],Z,W) is to divide the first list where X represents the head of the list and Y represents the tail of the list. Z is second Set. W is intersection of set.
list_member(X,Z) checks the X to be member of Z. If X is member of Z then Intersection is calculated on Y and Z including X being not a member of Z set.