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:
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.> (defstruct employee age first-name last-name sex children) EMPLOYEE
As with the other data types before, it is useful to associate particular instances with a symbol for easy access:
(Different implementations of Lisp will display structures in different ways.)> (setf employee1 (make-employee)) #S(EMPLOYEE AGE NIL FIRST-NAME NIL LAST-NAME NIL SEX NIL CHILDREN NIL)
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:
Slot values can be assigned using setf:> (employee-age employee1) NIL > (employee-sex employee1) NIL
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 (employee-age employee1) 56) 56 > (employee-age employee1) 56
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.> (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
Defstruct also allows you to specify default values for given slots. Here is an example:
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:> (defstruct trekkie (sex 'male) (intelligence 'high) age) TREKKIE
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:> (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)
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.> (typep t1 'employee) NIL > (typep t1 'trekkie) T > (trekkie-p t1) T > (employee-p t3) NIL
© Colin Allen & Maneesh Dhagat