Introduction to Sets
Pascal has a built-in set type that directly models mathematical sets of a base ordinal type, declared as 'type TVowelSet = set of Char;' or more narrowly 'set of 1..100', and set literals are written with square brackets, such as '['A','E','I','O','U']'. Because sets are a native language feature rather than a library, operations like membership, union, and intersection compile to efficient bitwise operations rather than loops.
Cricket analogy: A set of dismissed batsmen is like a scorer's quick tally sheet of who's already out — checking 'if Player in OutSet' answers 'has this batsman already been dismissed' instantly, without scanning the whole scorecard.
Set Operations
Pascal defines set union with '+', intersection with '*', and difference with '-', so given 'A := [1,2,3]; B := [2,3,4];', the expression 'A + B' yields [1,2,3,4], 'A * B' yields [2,3], and 'A - B' yields [1]. Membership is tested with 'in', as in 'if 3 in A then', and these operators make sets an elegant way to express conditions that would otherwise require chains of 'or' comparisons, such as replacing 'if (ch = 'a') or (ch = 'e') or (ch = 'i') then' with 'if ch in ['a','e','i'] then'.
Cricket analogy: Union is like combining the sets of players who scored a century and players who took a five-wicket haul into one 'all-rounders of the match' set — a single '+' replaces a long chain of 'or' checks.
Practical Constraints of Pascal Sets
Standard Pascal sets are typically limited to a small base range (historically often 0..255 elements, implementation-dependent), because the compiler represents a set as a bitmap where each possible element occupies one bit, so 'set of Byte' is common and efficient, while 'set of Integer' with a huge range is usually rejected or impractical. This bitmap representation is exactly why set operations are so fast, but it also means sets are unsuitable for representing large or sparse value ranges.
Cricket analogy: It's like a bowling figures tracker limited to overs 1 through 50 in an ODI — the bitmap only needs 50 bits, so it's efficient, but you couldn't use the same tiny bitmap to track something with millions of possible values like exact runs scored across a career.
type
TCharSet = set of Char;
var
vowels, letter: TCharSet;
ch: Char;
begin
vowels := ['a', 'e', 'i', 'o', 'u'];
write('Enter a letter: ');
readln(ch);
if ch in vowels then
writeln(ch, ' is a vowel')
else
writeln(ch, ' is a consonant');
end.The 'in' operator combined with a set literal is often the cleanest replacement for long chains of OR comparisons: 'if ch in ['a','e','i','o','u']' is both more readable and, on many compilers, faster than five separate equality checks joined by OR.
- Pascal sets model mathematical sets of an ordinal base type using bitmaps internally.
- Set literals use square brackets, e.g. ['A','E','I','O','U'].
- Union (+), intersection (*), and difference (-) are built-in set operators.
- The 'in' operator tests membership efficiently, replacing long OR chains.
- Sets are typically limited to small base ranges due to their bitmap representation.
- Sets are ideal for compact, fixed-range membership checks, not large or sparse ranges.
Practice what you learned
1. How is a set literal containing the letters a, e, and i written in Pascal?
2. Which operator computes the intersection of two sets A and B?
3. What does 'if ch in ['a','e','i','o','u'] then' test?
4. Why are Pascal set operations typically very fast?
Was this page helpful?
You May Also Like
Arrays in Pascal
Learn how Pascal's fixed-size, strongly-typed arrays work, from simple index ranges to multidimensional matrices.
Strings in Pascal
Learn how Pascal's length-prefixed strings work, from fixed-length capped strings to dynamic AnsiString, plus core string functions.
Records in Pascal
Understand how Pascal records bundle fields of different types into a single structured entity, including variant records.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics