Notes: Sets

The set type is the simplest. A set is an unordered collection of objects with objects appearring at most once in the set. Sets are useful when your data structure has no inherent order and is a simple collection of objects.

Creating Sets

There are two ways to create a set object. A set may be created by parsing a set literal or by calling an intrinsic function that returns a set object.

Set Literals

A literal set object is defined via the syntax,

[set <object-list>]

This creates, at parse time, a single set object containing the, zero or more, objects specified.

E. g.,

[set 1, "two", #three|3|3\.0#]

Creates a set containing three objects, the integer `1', the string "two" and a regular expression that matches threes.

Literals Are Single Objects

It is important to remember that a literal set is a single object and assigning it to a variable makes that variable reference the single set literal. It is generally an error to return a reference to a literal from a function that is intended to create new objects. This aspect of literal sets, and other aggregates, is one of ICI's most common programming errors.

Intrinsics That Return Sets

The second way of creating a set is to call an intrinsic function that returns a set. The most important of these is the set() function. Set() returns a new set object. Any arguments passed to set() are made the initial contents of the set.

Accessing Set Elements

Sets, like the other aggregate types, uses the indexing operation to access their members. Unlike the other types, however, sets have no real concept of a separate key and value. An element is either in a set or it isn't in the set. If anything, and this is ICI's approach, the element has a logical value, true if it is in the set and false if it is not in the set.

To add an object to a set you use the object as the key and assign that element some non-zero or non-NULL value. To remove an object you assign NULL or zero to that element.

Iterating Over A Set

The forall statement may be used to iterate over the elements of a set. The order of iteration is effectively random although two iterations over an unmodified set will always occur in the same order( i. e., a set is implemented as a dynamic hash table).

Set Operators

There is a full complement of set operators in ICI providing for the usual set manipulations. The operators overload certain arithmetic operators,

set + set Union.

set - set Difference.

set * set Intersection.

set < set Proper sub-set.

set <= set Sub-set.

set > set Proper super-set.

set >= set Super-set.

The sub- and super operators return logical results indicating if their first operand is a sub, or super, set of their second operand. The "proper" versions of these operators requires that the second set contain more than simply the first set, i. e., it is an actual sub-set.