Our pseudo-code design will assume we have sets:
enumerated type or subrange 0 -> N
type musictype=(classical,folk,rock)
musicset = set of musictype
var musicp,musicpref:musicset
[classical],[folk],[rock],[classical,folk],[classical,rock]
[rock,folk],[classical,rock,folk],[].
type letters = 'A'..'Z';
SetOfLetters = set of letters;
var vowels,consonants:set of letters;
:
vowels['A','E','I','O','U'];
consonants['A'..'Z']-vowels;
[1,2,3,4,5]=[1..5] specify subrange
[1,2,3,4,5,10]=[1..5,10] [classical..rock]
[5..1]=[] since 5>1
What operators can be used on/with sets?
Arithmetic operators:
+ = set union = adding elements to a set.
[3..5] + [1,2] = [1..5]
Note: musicprefmusicp + [classical]; /* Correct */
musicprefmusicp + classical; /* Incorrect */
Also [classical,folk,rock] + [folk] = [classical,folk,rock]
[classical,folk]-[folk] = [classical]
[classical,folk]-[rock] = [classical,folk]
[classical,folk]*[folk,rock] -> [folk]
[folk]*[rock] -> []
if element is not in set then deleting it, '-', changes nothing;
if it is already in Set, then adding it to set, '+', changes nothing.
These produce a boolean result: = true or false
= equality : do sets contain same elements?
[classical,jazz]=[jazz,classical] is true
[5..1]=[] is true since 5>1
[1]=[1,2] is false
[2..5] <= [1,2..5] is true
[1..3] <= [1,3] is false
[classical] >= [classical,folk,rock] is false
7 in [1..6] is false
7 in [1..7] is true
Note: not [7] in [1..7]
if (ch>='a') and (ch<='z') we could use
if ch in ['a'..'z']
Rutgers Pascal manual page A-2 item f says:
sets of char are restricted and in fact
['a'..'z'] is same as ['A'..'Z']. BE CAREFUL.
Therefore check implementation defined limits before you write a large "elegant" set program.
NOTE:
(1) A typical error is
Wrong
![]()
repeat
...........
until ch not in ['0'..'9'];
Correct form is
![]()
repeat
.........
until not ch in ['0'..'9'];
(2) Assume that we can only have sets of ordinal types. (It would be nice to have a set of some-type: set of pointers?)
(3) ch in ['A'..'Z'] will only work only if 'A' -> 'Z' is ordered and contiguous.