Home Up Contents Search

Sets

Sets, a collection of objects of the same ordinal type, are an elegant tool for expressing relationships in a program.

The base type of the set (the type of objects contained within it) must have 256 or fewer possible values. Thus, you can define a set of type char, or a set of the subrange 75..98. But you can't define a set of type integer, because it would have 65 536 possible values.

A set is created by enclosing within brackets a comma-delimited list of objects of the same ordinal type. If the list contains elements with consecutive ordinal values, you can use subrange notation (two expressions separated by two periods). Here are some examples:

[] empty set
[1,3,5,7,9] set of integer subrange
['A'..'Z'] set of char
[Mon,Wed..Fri] set of Days
[January..April, September..December] set of Months


Defining a Set Type
To define a set type, use the reserved words "set of", followed by the name of the base type. For instance:

type

CharSet = set of char; { Set of objects of the type char }
MonthDays = set of 1..31;
{ set of integer subrange }
DayType = (Sun, Mon, Tue, Wed, Thur, Fri, Sat);
Days = set of DayType;
{ Set of objects of DayType }

Colors = set of (Red, Green, Blue);
{-Set of objects of the anonymous enumerated type (Red, Green, Blue) }

var

wd : Days;

begin

wd = [Mon..Fri];


Sets can be processed with a number of special operators. Assuming the following sets:

set1 := ['A', 'B', 'C'];
set2 := ['C', 'D', 'E'];
set3 := ['A', 'B', 'C'];
set4 := ['A', 'B'];


The union operator (+) adds two sets, producing a third set:

set1 + set2 = ['A', 'B', 'C', 'D', 'E'];


The membership operator (in) tells whether one set is fully contained within a second set:

if ('A' in set1) then . . .


The difference operator (-) subtracts one set from another, that is, all members of the first set that aren't also members of the second set.

set1 - set2 = ['A', 'B']


The intersection operator (*) produces a set containing the values common to both sets:

set1 * set2 = ['C']


Some of the operators used with numbers and strings can be used on sets, but with different meanings.

The equality operator (=) returns a Boolean value indicating whether two sets are exactly the same:

set1 = set3 returns TRUE
set1 = set2 returns FALSE


The inequality operator (<>) returns a Boolean value indicating whether two sets are different:

set1 <> set3 returns FALSE
set1 <> set2 returns TRUE


The inclusion operators (<= and >=) return a Boolean value indicating whether a set is a subset of another set. The <= operator returns TRUE if the first set is a subset of the second set, and the >= operator returns TRUE if the second set is a subset of the first set.

set1 <= set2 returns FALSE
set4 <= set1 returns TRUE
set3 >= set2 returns FALSE
set1 >= set4 returns TRUE

program SetsDemo;

uses
Win32Crt;

var

Alphabet, Vowels, Consonants: set of char;
C: char;



function LCase(C: char): char;
{ return lowercase equivalent of parameter C }

begin

if C in ['A'..'Z'] then LCase := Chr(Ord(C) + 32) else LCase := C;

end; { LCase }



begin { SetsDemo }

Alphabet := ['a'..'z'];
Vowels := ['a', 'e', 'i', 'o', 'u'];
Consonants := Alphabet - Vowels;
repeat

Write('Type a letter: ');
C := LCase(ReadKey);
if C in Vowels then

Writeln('That character is a vowel')

else

if C in Consonants then Writeln('That character is a consonant')
else if C in [' '..'~'] then Writeln('That character is a printable ASCII character');

until C in [#3, #27, #13];

end. { SetsDemo }

 
 
Previous Next
Send mail to ljschwerin@hotmail.com with questions or comments about this web site.

Copyright © 1999-2002 Leon Schwerin
Last modified: 26 March 2000
1