% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\SetXmpTitle}{Set}
\newcommand{\SetXmpNumber}{9.71}
%
% =====================================================================
\begin{page}{SetXmpPage}{9.71 Set}
% =====================================================================
\beginscroll
%

The \spadtype{Set} domain allows one to represent explicit finite sets of values.
These are similar to lists, but duplicate elements are not allowed.
\xtc{
Sets can be created by giving a fixed set of values \ldots
}{
\spadpaste{s := set [x**2-1, y**2-1, z**2-1] \bound{s}}
}
\xtc{
or by using a collect form, just as for lists.
In either case, the set is formed from a finite collection of values.
}{
\spadpaste{t := set [x**i - i+1 for i in 2..10 | prime? i] \bound{t}}
}

\xtc{
The basic operations on sets are
\spadfunFrom{intersect}{Set}, \spadfunFrom{union}{Set},
\spadfunFrom{difference}{Set},
and \spadfunFrom{symmetricDifference}{Set}.
}{
\spadpaste{i := intersect(s,t)      \free{s t}\bound{i}}
}
\xtc{
}{
\spadpaste{u := union(s,t)          \free{s t}\bound{u}}
}
\xtc{
The set \spad{difference(s,t)} contains those members of \spad{s} which
are not in \spad{t}.
}{
\spadpaste{difference(s,t)          \free{s t}}
}
\xtc{
The set \spad{symmetricDifference(s,t)} contains those elements which are
in \spad{s} or \spad{t} but not in both.
}{
\spadpaste{symmetricDifference(s,t)          \free{s t}}
}

\xtc{
Set membership is tested using the \spadfunFrom{member?}{Set} operation.
}{
\spadpaste{member?(y, s)               \free{s}}
}
\xtc{
}{
\spadpaste{member?((y+1)*(y-1), s)     \free{s}}
}
\xtc{
The \spadfunFrom{subset?}{Set} function determines whether one set is a subset
of another.
}{
\spadpaste{subset?(i, s)               \free{i s}}
}
\xtc{
}{
\spadpaste{subset?(u, s)               \free{u s}}
}

\xtc{
When the base type is finite, the absolute complement of a set is
defined.
This finds the set of all multiplicative generators of
\spadtype{PrimeField 11}---the integers mod \spad{11.}
}{
\spadpaste{gs := set [g for i in 1..11 | primitive?(g := i::PF 11)] \bound{gs}}
}
\xtc{
The following values are not generators.
}{
\spadpaste{complement gs \free{gs}}
}

Often the members of a set are computed individually; in addition,
values can be inserted or removed from a set over the course of a
computation.
\xtc{
There are two ways to do this:
}{
\spadpaste{a := set [i**2 for i in 1..5] \bound{a}}
}
\xtc{
One is to view a set as a data structure and to apply updating operations.
}{
\spadpaste{insert!(32, a) \free{a}\bound{ainsert}}
}
\xtc{
}{
\spadpaste{remove!(25, a) \free{a}\bound{aremove}}
}
\xtc{
}{
\spadpaste{a \free{aremove ainsert}}
}
\xtc{
The other way is to view a set as a mathematical entity and to
create new sets from old.
}{
\spadpaste{b := b0 := set [i**2 for i in 1..5] \bound{b}}
}
\xtc{
}{
\spadpaste{b := union(b, {32})         \free{b}\bound{binsert}}
}
\xtc{
}{
\spadpaste{b := difference(b, {25})    \free{binsert}\bound{bremove}}
}
\xtc{
}{
\spadpaste{b0 \free{bremove}}
}

For more information about lists, see \downlink{`List'}{ListXmpPage}\ignore{List}.
\showBlurb{Set}
\endscroll
\autobuttons
\end{page}
%