Last Updated:

Sets in Pascal

Sets in Pascal

A set in Pascal refers to a collection of interrelated elements of the same type. Its elements must be of an ordinal data type and be described by spacing or enumeration. The service words Set O give a signal for a multiple-type declaration.

Type <typename> = Set Of <base type>;

About enumerated types

The three scalar enumerators of the boolean, char, and integer types can be specified as follows:

type
Boolean= (false, true);
char= #0..#255;
integer= -32768..32767;
(#xxx means that the character whose code in the ASCII table = xxx must be taken.)

They are basic types that do not need to be specified each time. They are already defined in the system in this way. In addition to them, there are several other interval types:

shortint>= -128..127;
longint= -2147483648..247483647;
byte= 0..255;
word= 0..65535;

 

Variables with these types can take values that lie within the boundaries of the corresponding intervals. The following functions are used with the enumerated types (they are stored in the library of system functions and the programmer has access to them):

 

  • ord(N) – returns the number of the element N in the set;
  • succ(N) – returns the following value for N;
  • pred(N) — returns the previous value for N.

The user can describe his or her enumerated data type. This description is summarized as follows:

type
<typename>=(<IDEnter1>,<IDEnter2>, ..,<IDENTN>);

All possible values that a variable of this type can take are listed in parentheses. Please note that the name of the values cannot be set in Russian. For example, you can describe the color data type and list the 7 primary colors:

type
color=(red, orange, yellow, green, blue, magenta);

Values receive numbers in the order in which they are listed (starting with 0). Therefore, for this type, there will be the following element relationship:

red< orange< yellow< green< blue< magenta;
ord(red)=0;
succ(blue)= magenta;
pred(green)=yellow;

Pascal Multiple Data Type

 

As for the pascal multiple data type, it resembles an enumerable one. Additionally, multiple is a set of elements that are not organized in order.
A multiple data type in mathematics is any combination of elements of an arbitrary nature. Operations performed on a set are essentially to include and exclude elements from the set.

A multiple type in Pascal refers to a finite collection of elements that belong to some underlying data type. In the role of base types can be used:

  • symbolic;
  • enumerated;
  • Byte;
  • Range-based listed above.

These limitations are related to the form of representation of a multiple data type in Pascal and may amount to the fact that the ord() function for the base type used lies in the range from 0 to 255.

When the base type is specified, the set of values of the multiple data type will be determined automatically. This includes all possible sets, which are arbitrary combinations of values of the base type. These sets are individual values of a particular multiple data type.

Description of the Pascal multiple data type

Type <typename>= set of <basetype>

Example of a multiple Pascal data type

Type symbol= set of char; {describes multiple symol type of letters}
Var letter, digits, sign: symbol; {three variables of multiple type are described}

To set the value of a variable of multiple type, use the set constructor - the elements of the set are listed in square brackets separated by commas. For example:

sign:= [‘+’, ‘-‘];

A set constructor can include a range of base type values. In this case, all the components of the range are included in the sets. For example:

digits:= [‘0’ .. ‘9’];
letter:= [‘a’ .. ‘z’];

In this case, both forms of designing sets are combined. Example:

letter:= [‘a’ .. ‘z’, ‘A’ .. ‘Z’];

A constructor of the form [] denotes empty sets. You can also use multiple types as constants in your program. In this case, they are defined as follows:

Const YesOrNo= [‘Y’, ‘y’, ‘N’, ‘n’];

An example of defining a multiple type as a thymized constant:

Const digits: set of char= [‘0’ .. ‘9’];

In this case, it is allowed to use the "+" sign, indicating the merger of sets. Example:

Const Yes= [‘Y’, ‘y’]; No= [‘N’, ‘n’];
YesOrNo= Yes+ No;

 

Operations on multiple Pascal types

 

Actions that can be performed with Pascal types include joining, intersecting, and excluding.


An association of multiple types consists of elements that belong to at least one set. And each element is only once included in the union. The merge operation is indicated by the "+" sign. Example:

Type symbol= set of char;
Var small, capital, latin: symbol;
..................
small:= [‘a’ .. ‘z’];
capital:= [‘A’ .. ‘Z’];
latin:= small + capital; {forms sets of Latin letters by combining sets small and capital}

An example of combining individual elements and multiple types:

small:= [‘c’ .. ‘z’];
small:= small + [‘a’] +[‘b’];

An exception is a multiple type difference where elements related to the subtractable are excluded from the reducible. If the subtracted includes elements that are not in the reducible, then they will not be able to affect the result. The exception is indicated by the "-" sign. Example:

letter:= [‘a’ .. ‘z’]; {sets of letters of the Latin alphabet}
glasn:= [‘a’, ‘e’, ‘o’, ‘u’, ‘i’, ‘y’]; {sets of vowel letters}
soglasn:= letter-glasn; {a plurality of consonant letters is formed by excluding vowel letters from the set of all letters}

The intersection of "*" is a set containing elements that are part of both sets at the same time. Example:

Type chisla= set of byte;
Var z,x,y: chisla;
...........
x:= [0..150];
y:= [100..255];
z:= x*y {obtained sets of numbers from the range 100..150 as a result of the intersection of two sets}

 

Pascal multiple type relation operations

 

Together with the considered operations, certain relation operations are also defined on the values of the multiple type. In general, the operands of operations on multiple values are multiple expressions. Among the operations of the relation on the values of the plural type, a special place is occupied by the operation of checking the entry of an element into the set, which is denoted by the characters in.

The first operand in an in operation must belong to a base type (unlike other relationship operations in which the values of two operands belong to the same multiple value type). The second operand must belong to a multiple value type that is built on the basis of this base type. The result of the relationship operation is a Boolean value of false or true.

'a' in glasn operation value true;
'o' in soglasn operation value false;

A comparison operation on the equality of multiple Pascal types. Sets are considered equivalent (equal) if all the elements of one set are in another and vice versa. For an inequality or equality comparison operation, the symbols '=' and '<>' are used.

A:= [2,1,3];
D:= [1,3,2];

In this case, the A=D operation is true and the A<>D operation is false. How do I check for inclusions? One set is considered to be included in another if all its elements are in the second set. However, the reverse statement may be unfair. The inclusion check is indicated as follows: '<=' and '>='.

letter >= glasn;
soglan <= letter;

It is worth noting that the use of < and > operations on multiple operands is unacceptable.