The utility of sets (in Pascal)

Paying around with Pascal recently, I had forgot about how nice its sets structures are. In addition to arrays and records, sets offer an alternative built-in data structure. For those that don’t know, a set is a collection of items of the same type – it is essentially a packaged, unindexed data structure. They are inherent flexible, and useful, largely because of the syntax. Operations are provided for set union, intersection, membership, and construction. For example:

var digit : set of '0'..'9'
    range : set of 1..100;
    alphabet : set of 'a'..'z';

defines a set, digit, that may contain any combination of the characters ‘0’ through ‘9’. The predicate:

x in digit

tests whether or not the value of the variable x is in the set digit. Here’s a good example of how sets can be used::

operandset, operatorset: set of char;
operatorset := ['%','+','-','*','/','^'];
operandset := ['a'..'z','0'..'9'];

This leads to very efficient statements, of the form:

if x in operandset
then write(' operand ')
else if x in (operatorset + ['(',')'])
     then write(' operator or parenthesis ')
     else write(' illegal symbol ')

Here’s the equivalent in Fortran, which has no similar structure:

if ((x >= 'a' .and. x <= 'z') .or. &
    (x >= '0' .and. x <= '9')) then
    write(*,*) "operand"
elseif (x == '%' .or. x == '+' .or. x == '-' .or. &
        x == '*' .or. x == '/' .or. x == '^' .or. &
        x == '(' .or. x == ')') then
    write(*,*) "operator or parenthesis"
    write(*,*) "illegal symbol"
end if

Now although many languages such as C++ provide support for sets in some form or another, rarely is it part of the core language. So languages such as Fortran, or C contain no real set structure. Why do I care about this? Because there are certain circumstances when a set can help reduce syntactic complexity, as the example above shows.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.