Previous | Table of Contents | Next |
6.2.4.4. Sets
A set is an unordered collection of distinct values. The function set() creates a set that initially is empty, as in
words := set()
The function insert(S, x) inserts x into the set S. For example,
insert(words, cornucopia)
adds cornucopia to words. If the value already is in the set, insert() does nothing.
The function delete(S, x) deletes the member x from the set S. For example,
delete(words, cornucopia)
deletes cornucopia from words, if it is contained in words. Attempting to delete a value that is not in a set does nothing.
The function member(S, x) succeeds if x is in S but fails otherwise.
The following operations on sets are available:
An example of the use of sets is the following code segment, which creates a set containing all the different letters in the input file:
letters := set() while line := read() do every insert(letters, !line)
6.2.4.5. Tables
A table is a collection of elements. An element is a pair, consisting of a key and a value associated with the key.
The function table(x) creates an empty table, where x is a default value that is associated with new keys. For example,
wordcount := table(0)
assigns to wordcount an empty table whose default value is 0.
Tables are subscripted like lists, but the subscripts are keys that can be any type of value. For example,
wordcount[cornucopia] := 1
assigns the value 1 to the key cornucopia in wordcount. Subsequently,
write(wordcount[[cornucopia])
writes 1.
The value associated with a key can be changed, as in
wordcount[[cornucopia] := 13
Augmented assignment is useful for changing the values associated with keys. For example,
wordcount[cornucopia] +:= 1
increments the value associated with the key cornucopia.
The size of a table increases by 1 every time a value is assigned to a key that is not already in the table. An element is added to a table only when an assignment is made to a new key. Therefore, if the key tumbler has not been assigned a value in wordcount, the expression
wordcount[tumbler]
produces the default value of 0, but tumbler is not added to wordcount and the size wordcount does not change. On the other hand,
wordcount[tumbler] +:= 1
adds tumbler to wordcount and increases the size of wordcount by 1.
An example of the use of tables is the following code segment, which creates a table containing all the different letters in the input file and the number of times each one occurs:
letter_count := table(0) while line := read() do every letter_count[!line] +:= 1
There are several other functions that apply to tables. The function key(T) generates the keys in T. The order of generation is not predictable. Note that it is always possible to get from a key to its corresponding value.
The functions insert(), delete(), and member() apply to tables as well as sets. The function member(T, x) succeeds if x is a key in the table T. The function insert(T, x, y) is equivalent to T[x] := y.
6.2.4.6. Operations on Structures
Several operations apply to all kinds of structures:
6.2.4.7. Serial Numbers
Every structure has a serial number, and each structure type has a separate series of serial numbers. Serial numbers start at 1 for the first structure created of that type and increase as new structures are created. Each record type has its own series.
The function serial(x) returns the serial number of the structure x.
6.2.4.8. Sorting Structures
A structure can be sorted to produce a list with the values in the structure in sorted order. Sorting is first by type, in the order null, integer, real, string, followed by other types. Sorting for numbers is in order of nondecreasing magnitude. Sorting for strings is in nondecreasing lexical (alphabetical) order.
Lists, sets, and records are sorted by sort(X), which produces a list with the values of X in sorted order.
Sorting tables is more complicated because a table element consists of a key and a value. The way a table is sorted depends on the second argument of sort():
The result of sorting often can be used directly. For example, to write all the letters in the set letters, all that is needed is
every write(!sort(letters))
In the case of tables, a sorted list with alternating keys and values can be conveniently formatted using queue access. As an example, to write a list of letters and their counts from letter_count, the following will do:
letter_list := sort(letter_count, 3) while write(get(letter_list), right(get(letter_list, 10))
The letters are followed by their counts right adjusted in a 10-character field with blanks for padding.
Previous | Table of Contents | Next |