
- 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 - Union of Set
In Prolog, we generally represents Sets using List. In this chapter, we're discussing how to get Union of two sets by defining a predicate.
Getting Union of Sets represented by a List
Let us define a clause called list_union(L1,L2,L3), So this will take L1 and L2, and perform Union on them, and store the result into L3. As you know if two lists have the same element twice, then after union, there will be only one. So we need another helper clause to check the membership.
Program (list_set.pl)
list_member(X,[X|_]). list_member(X,[_|TAIL]) :- list_member(X,TAIL). list_union([X|Y],Z,W) :- list_member(X,Z),list_union(Y,Z,W). list_union([X|Y],Z,[X|W]) :- \+ list_member(X,Z), list_union(Y,Z,W). list_union([],Z,Z).
Note − In the program, we have used (\+) operator, this operator is used for NOT.
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 compiled, 4 lines read - 1484 bytes written, 6 ms yes | ?- list_union([a,b,c,d,e],[a,e,i,o,u],L3). L3 = [b,c,d,a,e,i,o,u] ? yes | ?- list_union([a,b,c,d,e],[1,2],L3). L3 = [a,b,c,d,e,1,2] 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_union([X|Y],Z,W) :- list_member(X,Z),list_union(Y,Z,W) returns W as a union of first two Sets.
list_union([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 union of set.
list_member(X,Z) checks the X to be member of Z. If X is member of Z then Union is calculated on Y and Z excluding X being already member of Z set.
-
list_union([X|Y],Z,[X|W]) :- \+ list_member(X,Z), list_union(Y,Z,W) returns W as a union of first two Sets including X as head element.
list_union([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 union of set.
list_member(X,Z) checks the X to be member of Z. If X is member of Z then Union is calculated on Y and Z excluding X being already member of Z set.