Hi ! Nice to meet you again !This time, we'll discuss about :
Set's actually a special feature of Pascal that cannot be found in other programming languages. It merely took for granted most of the times, though it is very good to employ it in our program. Beside it helps much the readability, it ease our job in thinking about sets. It is intended to extend the array, but programmers seem to ignore it, prefer using array instead of sets.
Well, personally I also seldom use sets. I have a little knowledge of this, but I thought it was sufficient enough for practical use. Many books ignore this special feature, but I'll try to cover it as best and as clearly as possible.
What is sets exactly ? Sets in programming is quite the same as in mathematics. Sets in mathematics is like this :
A = { 1, 2, 3, 4, 5 }
So does sets. OK, how can we declare sets ?
type days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); var allday : set of days;
You cannot write its contents or read to add it. Well, how can we use it ? Run this program :
type days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); var allday : set of days; workday : set of Monday .. Friday; thisday : days; begin thisday:=Monday; if thisday in workday then writeln('This day I''m in work') else writeln('This day I''m on holiday'); end.
Now, change thisday:=Monday into thisday:=Saturday, and run it again. See the difference ? Look at the word in in if thisday in .... You know its function now ?
Suppose I have the variable of myday, it is defined as set of days, like this :
var myday : set of days;
Naturally, myday contents is Sunday, Monday, ... through Saturday. Suppose Wednesday is my bad day, so that I don't want it included inside myday. You do this :
exclude(myday,Wednesday);
Then again, excluding Friday :
exclude(myday,Friday);
After that, you change your mind, including Friday again :
include(myday,Friday);
Look at this program :
type days = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); var allday : set of days; myday : set of days; workday : set of Monday .. Friday; thisday : days; begin exclude(myday,Wednesday); exclude(myday,Friday); thisday:=Friday; if thisday in myday then writeln('Friday is just my day !') else writeln('Friday is not my day !'); include(myday,Friday); { Changing your mind } if thisday in myday then writeln('I change my mind : Friday is just my day !') else writeln('Friday is still not my day !'); end.
Yes, you're brilliant ! You may guess the output be like this :
Friday is not my day ! I change my mind : Friday is just my day !
Got the idea ? Good ! The word in is not just comparing one variable to a set but comparing sets, too. Look at this :
if workday in allday then { This is true since all elements in workday } : { exist in allday. } : if allday in workday then { This is false, because there are elements } : { in allday which are not exist in workday, } : { they are Sunday and Saturday. }
Look at workday definition :
var workday : set of Monday..Friday; begin include(workday,Saturday); { This is illegal, since it is out of range } end.
You cannot include elements that is out of range. But you may include elements more than once even you have already got it inside the set, like this :
include(allday,Monday); { Legal } include(allday,Monday); { Legal too }
You might not need the sets since it can be formed inside our program. Look at these legal statements :
thisday:=Tuesday; if thisday in [Monday..Friday] then { True } : :
or like this :
thisday:=Tuesday; if thisday in [Monday,Thursday..Saturday] { False } : :
This is the practical use :
uses crt; var c : char; begin write('Yes or no (Y/N) ?'); repeat c:=upcase(readkey); until c in ['Y','N']; end.
Run that program then figure it out ! Then, this program :
uses crt; type allletter = set of 'A'..'Z'; var c : char; vowel : set of 'A','E','I','O','U'; letter : set of allletter; begin write('Enter a vowel'); repeat c:=upcase(readkey); until c in vowel; write('Enter a letter'); repeat c:=upcase(readkey); until c in letter; end.
Practice a lot, that was good. This is quite a short lesson, but this time, no extra. Sorry :) and no quiz too. It is quite easy and I think you've got the most of the lesson.
Sayonara, my friend !
Where to go ?
Back to main page
Back to Pascal Tutorial Lesson 1 contents
Back to Chapter 10 about complex data types
To Chapter 12 about making custom units
My page of programming link
Contact me here
By : Roby Joehanes, © 1997, 2000