Next: Lists and Arrays Up: Simple types Previous: Simple types

## Sets

One of the most useful (and most often used) data types in Darwin is the set. The Darwin type set closely mirrors the mathematical concept: a set is either empty or has a finite number of distinct unordered items. You can think of a set as a special bag to keep objects. This bag has the (magic) ability to discard multiple copies of the same object. When objects are in the bag, there is no order placed upon them.

Sets are surrounded by the brace symbols `{`, `}`.1.2

```> empty := {};                            # this set is empty
empty := {}
> FirstSet := {1, 1, 2, 2, 3, 3, 3};
FirstSet := {1,2,3}
> SecondSet := {1, 2, 3};
SecondSet := {1,2,3}
```
The sets FirstSet and SecondSet are equal since both contain exactly the elements 1, 2, 3.
```> ascend := { 0, 5, 10, 15, 20 };
ascend := {0,5,10,15,20}
> descend := { 20, 15, 10, 5, 0 };
descend := {0,5,10,15,20}
```
Again, ascend and descend are equal because sets are unordered.

We can perform several operations on objects of this type including union, intersection, member, subset and minus. All of these operations correspond to the standard mathematical definitions.

```> cdn := {'beaver', 'moose', 'grizzlie', 'loon', 'cow', 'deer'}:
> ch := {'sheep', 'mouse', 'cow', 'deer'}:
> intersect(cdn, ch);
{cow,deer}
> union(cdn, ch);
{beaver,cow,deer,grizzlie,loon,moose,mouse,sheep}
> member('grizzlie', ch);
false
> minus(cdn, ch);
{beaver,grizzlie,loon,moose}
> minus(ch, cdn);
{mouse,sheep}
```
We are not limited to placing only integer and string items in set structures. We could place any type of data we desire including other set and list objects. When each element of a set has the same type, we say that the set is homogeneous, otherwise we say that it is heterogeneous.
```> mixed := {'salmon', {'coho', 'pink'},  0, 'grizzlies', 1000, 'mosquitos'};
mixed := {0,1000,grizzlies,mosquitos,salmon,{coho,pink}}
```
The set mixed contains objects of three types: string, integer, and set.

Next: Lists and Arrays Up: Simple types Previous: Simple types
Gaston Gonnet
1998-09-15