Sets

A set is a collection of elements of same type. Pascal allows defining the set data type. The elements in a set are called its members. In mathematics, sets are represented by enclosing the members within braces{}. However, in Pascal, set elements are enclosed within square brackets [], which are referred as set constructor.

Defining Set Types and Variables

Pascal Set types are defined as

type
set-identifier = set of base type;

Variables of set type are defined as

var
s1, s2, ...: set-identifier;

or,

s1, s2...: set of base type;

Examples of some valid set type declaration are −

type
Days = (mon, tue, wed, thu, fri, sat, sun);
Letters = set of char;
DaySet = set of days;
Alphabets = set of 'A' .. 'Z';
studentAge = set of 13..20;

Set Operators

You can perform the following set operations on Pascal sets.

Sr.NoOperations & Descriptions
1UnionThis joins two sets and gives a new set with members from both sets.
2DifferenceGets the difference of two sets and gives a new set with elements not common to either set.
3IntersectionGets the intersection of two sets and gives a new set with elements common to both sets.
4InclusionA set P is included in set Q, if all items in P are also in Q but not vice versa.
5Symmetric differenceGets the symmetric difference of two sets and gives a set of elements, which are in either of the sets and not in their intersection.
6InIt checks membership.

Following table shows all the set operators supported by Free Pascal. Assume that S1 and S2 are two character sets, such that −

S1 := ['a', 'b', 'c'];
S2 := ['c', 'd', 'e'];
OperatorDescriptionExample
+Union of two setsS1 + S2 will give a set[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Difference of two setsS1 – S2 will give a set[‘a’, ‘b’]
*Intersection of two setsS1 * S2 will give a set[‘c’]
><Symmetric difference of two setsS1 >< S2 will give a set [‘a’, ‘b’, ‘d’, ‘e’]
=Checks equality of two setsS1 = S2 will give the boolean value False
<>Checks non-equality of two setsS1 <> S2 will give the boolean value True
<=Contains (Checks if one set is a subset of the other)S1 <= S2 will give the boolean value False
IncludeIncludes an element in the set; basically it is the Union of a set and an element of same base typeInclude (S1, [‘d’]) will give a set[‘a’, ‘b’, ‘c’, ‘d’]
ExcludeExcludes an element from a set; basically it is the Difference of a set and an element of same base typeExclude (S2, [‘d’]) will give a set[‘c’, ‘e’]
InChecks set membership of an element in a set[‘e’] in S2 gives the boolean value True

Example

The following example illustrates the use of some of these operators −

Live Demo

program setColors;
type  
color = (red, blue, yellow, green, white, black, orange);  
colors = set of color;  
 
procedure displayColors(c : colors);  
const  
names : array [color] of String[7]  
  = ('red', 'blue', 'yellow', 'green', 'white', 'black', 'orange');  
var  
   cl : color;  
   s : String;  

begin  
   s:= ' ';  
   for cl:=red to orange do  
      if cl in c then  
      begin  
         if (s<>' ') then s :=s +' , ';  
         s:=s+names[cl];  
      end;  
   writeln('[',s,']');  
end;  
 
var  
   c : colors;  
 
begin  
   c:= [red, blue, yellow, green, white, black, orange];
   displayColors(c);

   c:=[red, blue]+[yellow, green]; 
   displayColors(c);  

   c:=[red, blue, yellow, green, white, black, orange] - [green, white];     
   displayColors(c);    

   c:= [red, blue, yellow, green, white, black, orange]*[green, white];     
   displayColors(c);  

   c:= [red, blue, yellow, green]><[yellow, green, white, black]; 
   displayColors(c);  
end.

When the above code is compiled and executed, it produces the following result −

[ red , blue , yellow , green , white , black , orange]
[ red , blue , yellow , green]
[ red , blue , yellow , black , orange]
[ green , white]
[ red , blue , white , black]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *