aboutsummaryrefslogtreecommitdiff
path: root/src/hyper/pages/ALIST.ht
blob: 897d022ed1c0897c578c1bd0acec82d574bc8649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
\newcommand{\AssociationListXmpTitle}{AssociationList}
\newcommand{\AssociationListXmpNumber}{9.1}
%
% =====================================================================
\begin{page}{AssociationListXmpPage}{9.1 AssociationList}
% =====================================================================
\beginscroll
%

The \spadtype{AssociationList} constructor provides a general structure for
associative storage.
%-% \HDindex{list!association}{AssociationListXmpPage}{9.1}{AssociationList}
This type provides association lists in which data objects can be saved
%-% \HDindex{association list}{AssociationListXmpPage}{9.1}{AssociationList}
according to keys of any type.
For a given association list, specific types must be chosen for the keys and
entries.
You can think of the representation of an association list as a list
of records with key and entry fields.

Association lists are a form of table and so most of the operations available
%-% \HDindex{table}{AssociationListXmpPage}{9.1}{AssociationList}
for \spadtype{Table} are also available for \spadtype{AssociationList}.
They can also be viewed as lists and can be manipulated accordingly.

\xtc{
This is a \pspadtype{Record} type with age and gender fields.
}{
\spadpaste{Data := Record(monthsOld : Integer, gender : String) \bound{Data}}
}
\xtc{
In this expression, \spad{al} is declared to be an association
list whose keys are strings and whose entries are the above records.
}{
\spadpaste{al : AssociationList(String,Data) \free{Data}\bound{al}}
}
\xtc{
The \spadfunFrom{table}{AssociationList} operation is used to create
an empty association list.
}{
\spadpaste{al := table() \free{al}\bound{al1}}
}
\xtc{
You can use assignment syntax to add things to the association list.
}{
\spadpaste{al."bob" := [407,"male"]\$Data \free{al1}\bound{al2}}
}
\xtc{
}{
\spadpaste{al."judith" := [366,"female"]\$Data \free{al2}\bound{al3}}
}
\xtc{
}{
\spadpaste{al."katie" := [24,"female"]\$Data \free{al3}\bound{al4}}
}
\xtc{
Perhaps we should have included a species field.
}{
\spadpaste{al."smokie" := [200,"female"]\$Data \free{al4}\bound{al5}}
}
\xtc{
Now look at what is in the association list.
Note that the last-added (key, entry) pair is at the beginning of the list.
}{
\spadpaste{al \free{al5}}
}
\xtc{
You can reset the entry for an existing key.
}{
\spadpaste{al."katie" := [23,"female"]\$Data \free{al5}\bound{al6}}
}
\xtc{
Use \spadfunFromX{delete}{AssociationList} to destructively remove
an element of the association list.
Use \spadfunFrom{delete}{AssociationList} to return a copy of the
association list with the element deleted.
The second argument is the index of the element to delete.
}{
\spadpaste{delete!(al,1) \free{al6}\bound{al7}}
}

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