1 @c -----------------------------------------------------------------------------
3 @node Structures, , Arrays, Data Types and Structures
5 @c -----------------------------------------------------------------------------
8 * Introduction to Structures::
9 * Functions and Variables for Structures::
12 @c -----------------------------------------------------------------------------
13 @node Introduction to Structures, Functions and Variables for Structures, Structures, Structures
14 @subsection Introduction to Structures
15 @c -----------------------------------------------------------------------------
17 Maxima provides a simple data aggregate called a structure.
18 A structure is an expression in which arguments are identified by name (the field name)
19 and the expression as a whole is identified by its operator (the structure name).
20 A field value can be any expression.
22 A structure is defined by the @code{defstruct} function;
23 the global variable @code{structures} is the list of user-defined structures.
24 The function @code{new} creates instances of structures.
25 The @code{@@} operator refers to fields.
26 @code{kill(@var{S})} removes the structure definition @var{S},
27 and @code{kill(@var{x}@@ @var{a})} unbinds the field @var{a} of the structure instance @var{x}.
29 In the pretty-printing console display (with @mref{display2d} equal to @code{true}),
30 structure instances are displayed with the value of each field
31 represented as an equation, with the field name on the left-hand side
32 and the value on the right-hand side.
33 (The equation is only a display construct; only the value is actually stored.)
34 In 1-dimensional display (via @code{grind} or with @mref{display2d} equal to @code{false}),
35 structure instances are displayed without the field names.
37 There is no way to use a field name as a function name,
38 although a field value can be a lambda expression.
39 Nor can the values of fields be restricted to certain types; any field can be assigned any kind of expression.
40 There is no way to make some fields accessible or inaccessible in different contexts;
41 all fields are always visible.
43 @c -----------------------------------------------------------------------------
44 @node Functions and Variables for Structures, , Introduction to Structures, Structures
45 @subsection Functions and Variables for Structures
46 @c -----------------------------------------------------------------------------
48 @c -----------------------------------------------------------------------------
50 @defvr {Global variable} structures
52 @code{structures} is the list of user-defined structures defined by @code{defstruct}.
54 @opencatbox{Categories:}
56 @category{Global variables}
60 @c -----------------------------------------------------------------------------
62 @deffn {Function} defstruct @
63 @fname{defstruct} (@var{S}(@var{a_1}, @dots{}, @var{a_n})) @
64 @fname{defstruct} (@var{S}(@var{a_1} = @var{v_1}, @dots{}, @var{a_n} = @var{v_n}))
66 Define a structure, which is a list of named fields @var{a_1}, @dots{},
67 @var{a_n} associated with a symbol @var{S}.
68 An instance of a structure is just an expression which has operator @var{S}
69 and exactly @code{n} arguments.
70 @code{new(@var{S})} creates a new instance of structure @var{S}.
72 An argument which is just a symbol @var{a} specifies the name of a field.
73 An argument which is an equation @code{@var{a} = @var{v}} specifies the field name @var{a}
74 and its default value @var{v}.
75 The default value can be any expression.
77 @code{defstruct} puts @var{S} on the list of user-defined structures, @code{structures}.
79 @code{kill(@var{S})} removes @var{S} from the list of user-defined structures,
80 and removes the structure definition.
85 @c defstruct (foo (a, b, c));
88 @c defstruct (bar (v, w, x = 123, y = %pi));
95 (%i1) defstruct (foo (a, b, c));
101 (%i4) defstruct (bar (v, w, x = 123, y = %pi));
102 (%o4) [bar(v, w, x = 123, y = %pi)]
104 (%o5) [foo(a, b, c), bar(v, w, x = 123, y = %pi)]
106 (%o6) bar(v, w, x = 123, y = %pi)
110 (%o8) [bar(v, w, x = 123, y = %pi)]
113 @opencatbox{Categories:}
114 @category{Structures}
118 @c -----------------------------------------------------------------------------
120 @deffn {Function} new @
121 @fname{new} (@var{S}) @
122 @fname{new} (@var{S} (@var{v_1}, @dots{}, @var{v_n}))
124 @code{new} creates new instances of structures.
126 @code{new(@var{S})} creates a new instance of structure @var{S}
127 in which each field is assigned its default value, if any,
128 or no value at all if no default was specified in the structure definition.
130 @code{new(@var{S}(@var{v_1}, ..., @var{v_n}))} creates a new instance of @var{S}
131 in which fields are assigned the values @var{v_1}, @dots{}, @var{v_n}.
136 @c defstruct (foo (w, x = %e, y = 42, z));
138 @c new (foo (1, 2, 4, 8));
141 (%i1) defstruct (foo (w, x = %e, y = 42, z));
142 (%o1) [foo(w, x = %e, y = 42, z)]
144 (%o2) foo(w, x = %e, y = 42, z)
145 (%i3) new (foo (1, 2, 4, 8));
146 (%o3) foo(w = 1, x = 2, y = 4, z = 8)
149 @opencatbox{Categories:}
150 @category{Structures}
154 @c -----------------------------------------------------------------------------
157 @code{@@} is the structure field access operator.
158 The expression @code{@var{x}@@ @var{a}} refers to the value of field @var{a} of the structure instance @var{x}.
159 The field name is not evaluated.
161 If the field @var{a} in @var{x} has not been assigned a value,
162 @code{@var{x}@@ @var{a}} evaluates to itself.
164 @code{kill(@var{x}@@ @var{a})} removes the value of field @var{a} in @var{x}.
169 @c defstruct (foo (x, y, z));
170 @c u : new (foo (123, a - b, %pi));
179 (%i1) defstruct (foo (x, y, z));
181 (%i2) u : new (foo (123, a - b, %pi));
182 (%o2) foo(x = 123, y = a - b, z = %pi)
188 (%o5) foo(x = 123, y = a - b, z = %e)
192 (%o7) foo(x = 123, y = a - b, z)
197 The field name is not evaluated.
200 @c defstruct (bar (g, h));
210 (%i1) defstruct (bar (g, h));
228 @opencatbox{Categories:}
229 @category{Structures}