next up previous
Contents Next: Exercises. Up: Simple Data Structures Previous: Strings

Defstruct

Defstruct allows you to create your own data structures and automatically produces functions for accessing the data. Structures have names and ``slots.'' The slots are used for storing specific values. Defstruct creates a generic type of which particular ``instances'' can be made. Here is an example using defstruct to establish a structure type:

> (defstruct employee
   age
   first-name
   last-name
   sex
   children)
EMPLOYEE
In this example ``employee'' is the name of the structure, and ``age'', etc. are the slots. Defstruct automatically generates a function to make instances of the named structure. In this example the function is called make-employee, and in general the name of the instance constructor function is make-defstructname.

As with the other data types before, it is useful to associate particular instances with a symbol for easy access:

> (setf employee1 (make-employee))
#S(EMPLOYEE AGE NIL FIRST-NAME NIL LAST-NAME NIL SEX NIL 
     CHILDREN NIL)
(Different implementations of Lisp will display structures in different ways.)

In this case, employee1 is an instance of the type employee, and all its slots are initially given the value nil. Each slot is provided an automatic access function, by joining the structure name with the slot name:

> (employee-age employee1)
NIL

> (employee-sex employee1)
NIL
Slot values can be assigned using setf:

> (setf (employee-age employee1) 56)
56

> (employee-age employee1)
56
It is also possible to assign values to the slots of a particular instance at the time the instance is made, simply by preceding the slot name with a colon, and following it with the value for that slot:

> (setf employee2 (make-employee :age 34
                                :last-name 'farquharson
                                :first-name 'alice
                                :sex 'female))
#S(EMPLOYEE AGE 34 FIRST-NAME ALICE LAST-NAME FARQUHARSON 
      SEX FEMALE CHILDREN NIL)

> (employee-first-name employee2)
ALICE
As this example shows, it is not necessary to give values to all the slots when the make function is called. Neither is it necessary to specify slot values in the same order they are specified in the original defstruct.

Defstruct also allows you to specify default values for given slots. Here is an example:

> (defstruct trekkie
    (sex 'male)
    (intelligence 'high)
    age)
TREKKIE
The values enclosed in parentheses with a slot name are the default values for that slot -- that is, the values that these slots will have for created instance, unless explicitly overridden. These three examples of instances illustrate the use of defaults:

> (setf t1 (make-trekkie))
#S(TREKKIE SEX MALE INTELLIGENCE HIGH AGE NIL)

> (setf t2 (make-trekkie :age 35))
#S(TREKKIE SEX MALE INTELLIGENCE HIGH AGE 35)

> (setf t3 (make-trekkie :age 28 :sex 'female))
#S(TREKKIE SEX FEMALE INTELLIGENCE HIGH AGE 28)
Each instance of a structure has a type which can be tested with the predicate typep, or with the particular predicate automatically set up by defstruct. By default, the type of an instance is determined by the structure name:

> (typep t1 'employee)
NIL

> (typep t1 'trekkie)
T

> (trekkie-p t1)
T

> (employee-p t3)
NIL
There are several advanced features of defstruct, including the ability to create structures which incorporate other structures. If you understand the basics laid out here, however, you will have no trouble understanding the description of these features in Steele's Common Lisp the language.



next up previous
Contents Next: Exercises. Up: Simple Data Structures Previous: Strings



© Colin Allen & Maneesh Dhagat
March 2007