Previous Next Contents

15. Structures

LISP structures are analogous to C structs or Pascal records. Here is an example:

> (defstruct foo
    bar
    baaz
    quux)
FOO

This example defines a data type called foo which is a structure containing 3 fields. It also defines 4 functions which operate on this data type: make-foo, foo-bar, foo-baaz, and foo-quux. The first one makes a new object of type foo; the others access the fields of an object of type foo. Here is how to use these functions:

> (make-foo)
#s(FOO :BAR NIL :BAAZ NIL :QUUX NIL) 
> (make-foo :baaz 3)
#s(FOO :BAR NIL :BAAZ 3 :QUUX NIL) 
> (foo-bar *)
NIL
> (foo-baaz **)
3

The make-foo function can take a keyword argument for each of the fields a structure of type foo can have. The field access functions each take one argument, a structure of type foo, and return the appropriate field.

See below for how to set the fields of a structure.


Previous Next Contents