A small Pascal program prints Class A: 3 absent and Class B: 2 absent, and then announces that Class B has more absentees. Both counts are right and the verdict is wrong. The program compiled without a single warning, because the comparison you reached for from your array habits is legal Pascal that means something else. This tutorial uses Free Pascal 3.2.2 on Linux, and it assumes you have finished arrays and records.
Key takeaways
- A
SET OFanswers membership, not order and not size:>=asks whether one set contains every element of the other. - Free Pascal refuses
>and<on sets withError: Operation not implemented for sets, so the wrong question usually reaches the compiler as>=and compiles. - Counting the entries in a set takes a loop over its base type; Pascal gives a set no
Length. - A set is a 4-byte mask up to 32 elements and 32 bytes above that, whatever range you declared.
- The base type has to be an ordinal in 0..255, so student IDs above 255 belong in an array, not a set.
What a set keeps and what it drops
A SET OF answers exactly one question: is this value a member? It keeps no order, no duplicates and no count. An array gives you positions and a length, so a set feels thin by comparison, and that thinness is the point.
The base type must be an ordinal, such as a small integer range or char. Free Pascal stores the set as a mask with one bit per possible value, and the size depends on how many values the type allows, not on how many you put in:
| Declaration | sizeof |
|---|---|
set of 0..9 |
4 bytes |
set of 0..31 |
4 bytes |
set of 0..32 |
32 bytes |
set of 1..40 |
32 bytes |
set of char |
32 bytes |
Up to 32 possible values the set is a 4-byte mask. One value more and it is 32 bytes. Notice that nothing in the table is a count of members, and Pascal gives a set no Length.
Adding, removing, testing
Include adds a value, Exclude removes one, and in tests membership. This program checks its own results, so every line should print TRUE or FALSE:
program edit;
var
present: set of 1..40;
begin
present := [1..5];
Include(present, 7);
Include(present, 12);
Include(present, 12);
writeln('added 7, 12, 12 to [1..5] : ', present = [1, 2, 3, 4, 5, 7, 12]);
Exclude(present, 3);
writeln('after Exclude(present, 3) : ', present = [1, 2, 4, 5, 7, 12]);
writeln('is 7 present : ', 7 in present);
writeln('is 3 present : ', 3 in present);
writeln('present - [1, 2] = [4,5,7,12]: ', present - [1, 2] = [4, 5, 7, 12]);
end.
$ fpc edit.pas && ./edit
added 7, 12, 12 to [1..5] : TRUE
after Exclude(present, 3) : TRUE
is 7 present : TRUE
is 3 present : FALSE
present - [1, 2] = [4,5,7,12]: TRUE
Including 12 twice changed nothing, because a set records only whether a value is present, not how many times you added it. Include and Exclude edit one value in place, while + and - do the same job to whole sets.
The comparison that is not a count
Coming from arrays, you will want to ask which absent list is bigger. Try it:
program compare;
var
absentA, absentB: set of 1..40;
begin
absentA := [1, 2, 3];
absentB := [5, 6];
if absentA > absentB then
writeln('Class A has more absentees');
end.
$ fpc compare.pas
compare.pas(9,14) Error: Operation not implemented for sets
compare.pas(12) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Good: the compiler refuses. The trap is the next move, which is to try >= instead. That compiles, because >=, <=, = and <> all exist on sets. With a as [1, 2, 3], b as [5, 6] and c as [1, 2], I measured a >= b as FALSE, a <= b as FALSE, a = b as FALSE, a <> b as TRUE and c <= a as TRUE.
Read the last one: c <= a is true because every element of c is also in a. So >= means "contains every element of", not "has more elements". Here a >= b is false because a lacks 5 and 6, which says nothing about which list is longer.
Two files, one command
Here is the starter program. It counts correctly and then compares the sets themselves:
program attendance;
type
ClassSet = set of 1..40;
function Count(k: ClassSet): integer;
var
i: integer;
begin
Count := 0;
for i := 1 to 40 do
if i in k then
Count := Count + 1;
end;
var
absentA, absentB: ClassSet;
begin
absentA := [1, 2, 3];
absentB := [5, 6];
writeln('Class A: ', Count(absentA), ' absent');
writeln('Class B: ', Count(absentB), ' absent');
if absentA >= absentB then
writeln('Class A has more absentees')
else
writeln('Class B has more absentees');
end.
Count is the loop that stands in for the missing Length: it walks the base type and tests each value. Rather than trusting your eyes, save this checker as check_attendance.pas and compile it once with fpc check_attendance.pas:
program check_attendance;
var
expected: array[1..3] of string;
line: string;
i, passed, failed: integer;
begin
expected[1] := 'Class A: 3 absent';
expected[2] := 'Class B: 2 absent';
expected[3] := 'Class A has more absentees';
passed := 0;
failed := 0;
i := 0;
while not eof do
begin
readln(line);
i := i + 1;
if i <= 3 then
begin
if line = expected[i] then
begin
writeln('PASS line ', i, ': ', expected[i]);
passed := passed + 1;
end
else
begin
writeln('FAIL line ', i, ': expected "', expected[i], '", got "', line, '"');
failed := failed + 1;
end;
end;
end;
if i < 3 then
begin
writeln('FAIL the program printed ', i, ' lines, expected 3');
failed := failed + 1;
end;
writeln;
writeln(passed, ' of 3 checks passed');
if failed > 0 then
ExitCode := 1;
end.
Now fpc attendance.pas writes the binary attendance next to the source, and one command pipes it into the checker:
$ ./attendance | ./check_attendance
PASS line 1: Class A: 3 absent
PASS line 2: Class B: 2 absent
FAIL line 3: expected "Class A has more absentees", got "Class B has more absentees"
2 of 3 checks passed
The counts pass and the verdict fails. The fix is one line: compare the counts, not the sets. The rest of the file is unchanged:
program attendance;
type
ClassSet = set of 1..40;
function Count(k: ClassSet): integer;
var
i: integer;
begin
Count := 0;
for i := 1 to 40 do
if i in k then
Count := Count + 1;
end;
var
absentA, absentB: ClassSet;
begin
absentA := [1, 2, 3];
absentB := [5, 6];
writeln('Class A: ', Count(absentA), ' absent');
writeln('Class B: ', Count(absentB), ' absent');
if Count(absentA) > Count(absentB) then
writeln('Class A has more absentees')
else
writeln('Class B has more absentees');
end.
$ ./attendance | ./check_attendance
PASS line 1: Class A: 3 absent
PASS line 2: Class B: 2 absent
PASS line 3: Class A has more absentees
3 of 3 checks passed
Where sets stop
A set base type must be an ordinal that fits in 0..255. Declare a wider range and the compiler stops you:
program limit;
var
present: set of 0..999;
begin
present := [];
end.
$ fpc limit.pas
limit.pas(4,25) Error: illegal type declaration of set elements
limit.pas(9) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
So student IDs above 255 do not belong in a set. Use an array of boolean indexed by the ID instead, and count its TRUE entries with the same kind of loop. If your members are characters, set of char works, and you test the character itself with in.
Break it on purpose
Add student 4 to absentA with absentA := [1, 2, 3, 4]; and the checker fails line 1, because the count is now 4. Then put if absentA >= absentB then back, and line 3 goes red again.
Use a set when the question is whether a value is in a group, and compare sets with >= only when you mean "contains all of". When the question is how many, loop, count, and compare the counts.