[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

39. Structures


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

39.1 Introduction to Structures

Maxima provides a simple data aggregate called a structure. A structure is an expression in which arguments are identified by name (the field name) and the expression as a whole is identified by its operator (the structure name). A field value can be any expression.

A structure is defined by the defstruct function; the global variable structures is the list of user-defined structures. The function new creates instances of structures. The @ operator refers to fields. kill(S) removes the structure definition S, and kill(x@ a) unbinds the field a of the structure instance x.

In the pretty-printing console display (with display2d equal to true), structure instances are displayed with the value of each field represented as an equation, with the field name on the left-hand side and the value on the right-hand side. (The equation is only a display construct; only the value is actually stored.) In 1-dimensional display (via grind or with display2d equal to false), structure instances are displayed without the field names.

There is no way to use a field name as a function name, although a field value can be a lambda expression. Nor can the values of fields be restricted to certain types; any field can be assigned any kind of expression. There is no way to make some fields accessible or inaccessible in different contexts; all fields are always visible.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

39.2 Functions and Variables for Structures

Global variable: structures

structures is the list of user-defined structures defined by defstruct.

Categories:  Structures · Global variables

Function: defstruct (S(a_1, ..., a_n))
Function: defstruct (S(a_1 = v_1, ..., a_n = v_n))

Define a structure, which is a list of named fields a_1, ..., a_n associated with a symbol S. An instance of a structure is just an expression which has operator S and exactly n arguments. new(S) creates a new instance of structure S.

An argument which is just a symbol a specifies the name of a field. An argument which is an equation a = v specifies the field name a and its default value v. The default value can be any expression.

defstruct puts S on the list of user-defined structures, structures.

kill(S) removes S from the list of user-defined structures, and removes the structure definition.

Examples:

(%i1) defstruct (foo (a, b, c));
(%o1)                    [foo(a, b, c)]
(%i2) structures;
(%o2)                    [foo(a, b, c)]
(%i3) new (foo);
(%o3)                     foo(a, b, c)
(%i4) defstruct (bar (v, w, x = 123, y = %pi));
(%o4)             [bar(v, w, x = 123, y = %pi)]
(%i5) structures;
(%o5)      [foo(a, b, c), bar(v, w, x = 123, y = %pi)]
(%i6) new (bar);
(%o6)              bar(v, w, x = 123, y = %pi)
(%i7) kill (foo);
(%o7)                         done
(%i8) structures;
(%o8)             [bar(v, w, x = 123, y = %pi)]

Categories:  Structures

Function: new (S)
Function: new (S (v_1, ..., v_n))

new creates new instances of structures.

new(S) creates a new instance of structure S in which each field is assigned its default value, if any, or no value at all if no default was specified in the structure definition.

new(S(v_1, ..., v_n)) creates a new instance of S in which fields are assigned the values v_1, ..., v_n.

Examples:

(%i1) defstruct (foo (w, x = %e, y = 42, z));
(%o1)              [foo(w, x = %e, y = 42, z)]
(%i2) new (foo);
(%o2)               foo(w, x = %e, y = 42, z)
(%i3) new (foo (1, 2, 4, 8));
(%o3)            foo(w = 1, x = 2, y = 4, z = 8)

Categories:  Structures

Operator: @

@ is the structure field access operator. The expression x@ a refers to the value of field a of the structure instance x. The field name is not evaluated.

If the field a in x has not been assigned a value, x@ a evaluates to itself.

kill(x@ a) removes the value of field a in x.

Examples:

(%i1) defstruct (foo (x, y, z));
(%o1)                    [foo(x, y, z)]
(%i2) u : new (foo (123, a - b, %pi));
(%o2)           foo(x = 123, y = a - b, z = %pi)
(%i3) u@z;
(%o3)                          %pi
(%i4) u@z : %e;
(%o4)                          %e
(%i5) u;
(%o5)            foo(x = 123, y = a - b, z = %e)
(%i6) kill (u@z);
(%o6)                         done
(%i7) u;
(%o7)              foo(x = 123, y = a - b, z)
(%i8) u@z;
(%o8)                          u@z

The field name is not evaluated.

(%i1) defstruct (bar (g, h));
(%o1)                      [bar(g, h)]
(%i2) x : new (bar);
(%o2)                       bar(g, h)
(%i3) x@h : 42;
(%o3)                          42
(%i4) h : 123;
(%o4)                          123
(%i5) x@h;
(%o5)                          42
(%i6) x@h : 19;
(%o6)                          19
(%i7) x;
(%o7)                    bar(g, h = 19)
(%i8) h;
(%o8)                          123

Categories:  Structures · Operators


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on May, 5 2011 using texi2html 1.76.