Structure

A structure is a group of attribute values with each one associated with an identifier. It is very easy to create by using a pair of braces. Each attribute is declared and initialized by assigning the value to the identifier, and all attributes are enclosed in a pair of braces. For example

     dims = {length = 15, width = 10, height = 9};
Here a structure with three attributes is created. Note that each part of the definition corresponds to a structure attribute. The identifier on the left side of the assignment sign declares the name of an attribute, and will not be confused with any local variable in the surrounding scope. The expression to the right of the assignment sign is evaluated in the surrounding scope. For example
     length = 17
     height = 28;
     dims = {length = 15, width = length, height = height};
In definition of the dims structure, when width = length is processed, width declares a new structure attribute, where length is the value of the local variable length and has nothing to do with the previously defined attribute of the same structure, which is also named length. In height = height, the first height declares a new attribute, while the second will be the value of the local variable height.

An attribute of a structure can be accessed, added or reset using the dot operator .

>> dims = {length = 15, width = 10, height = 9};
>> dims.height
   9
>> dims.height = 12
>> dims.height
   12

Note that since function is also a data type, attributes of a structure can be functions as well. For example

>> region = {
              shape  = "circular",
              center = [2, -3],
              radius = 15,
              verify = (x, y) -> (x - 2)^2 + (y + 3)^2 <= 225
          };

>> region.verify(3, 2)
   1

One obvious use of structure is to return more than one values in a function. For example

     dstats = function x -> s
                   n = length(x);
                   min = max = sum = x[1];
                   for k = 2 : n
                       sum += x[k]; 
                       if x[k] < min
                            min = x[k];
                       end
                       if x[k] > max
                            max = x[k];
                       end
                   end
                   mean = sum / n;
                   sd = 0;
                   for k = 1 : n
                        sd += (x[k] - mean)^2;
                   end

                   sd = sqrt(sd / (n - 1));                   
                   
                   s = {
                          n = n, min = min, max = max,
                          mean = mean, sd = sd
                       };
               end
      x = rand(15);
      dstats(x)

In order to make a copy of a structure, we only need to assign the structure to another variable.

>> dims = {length = 15, width = 10, height = 9};
>> newdims = dims;
>> newdims.width = 30;
>> dims.width
   10
>> newdims.width
   30

A structure encapsulate several pieces of data and behaves like a class member. One may regard it as an object without a class, or a quicker way to create an object. Structures lack the more advanced features of classes, but are often sufficient and more convenient. Another important use of these classless objects is to provide `candidates' for conditional classes (chapter 8). They can ``apply'' for memberships of conditional classes and then be able to use the rich functionalities offered by the classes.

oz 2009-12-22