Lisp Structures

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 a comparison of LISP's defstruct and C's struct.

One difficulty with structures are the dashes. Almost all of their variable names use dashes, but dashes are also part of the syntax for accessing structure fields (or structure elements as they're called in C). As you read, be sure to notice what dashes are part of variable names and which ones are operators.

Generally structures in LISP are a lot like structures in C.

Lisp

C / C++

   (defstruct <structure name> 
        (<field name> <default value>)
        (<field name> <default value>)
        (<field name> <default value>)
   )

   
   typedef struct{
        <type> <field name> <default value> ;
        <type> <field name> <default value> ;
        <type> <field name> <default value> ;
   } <structure name> ;
   

Examples

   >(defstruct student
     (gender)
     (age) 
     (first_name nil)
     (last_name nil)
     (grade_level 11))
   typedef struct {
        int    Gender;
        int    Age;
        int    First_Name;
        int    Last_Name;
        int    grade_level;
   }Student;
   

Constructor

When a call to defstruct is made,  LISP automatically creates a 
constructor. It is
                    make-followed by the structure name. 

(setf <variable name> (make-<structure name>))

Example: 
>(setf SN170345 (make-student))

#S(STUDENT :GENDER NIL :AGE NIL :FIRST_NAME NIL :LAST_NAME NIL
:GRADE_LEVEL 11)

(Different implementations of LISP will display structures in different ways)

In this case, sn170345 is an instance of the type Student, and all its slots 
except grade-level are initially given the value nil.

    
Slot values can be assigned using setf: 
     >(setf (student-age sn170345) 56)
     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 sn170345 (make-student :gender 'female  :age 16))
                                                    
  #S(STUDENT-GENDER FEMALE AGE 16 )
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. 


In this example "student" is the name of the structure and gender, age, first_name, last_name and grade_level are the slots. In the sample the slots are given default values such as nil for first_name and 11 for grade_level "nice". The default values are enclosed in parentheses with the slot name.) It is not required that slots be given default values.

Defstruct creates a constructor,
                  a reader,
                  a writer,  and
                  a predicate to test if a variable is a  particular structure.

 Remember, like a structure in C, defstruct does not create an  instance of
 a structure, it only sets up the data type for memory, etc. To create an instance,
 use a constructor. 

Reader

Each slot is provided an automatic access function, by joining the structure name
 with the slot name: 

 To access data stored in a field of a structure: 
(<structure name> - <field name> <instance>)

Example: 
    >(student-gender sn170345)
     female
     >(student-grade_level sn170345)
     11
    >(student-age sn170345)
    16

Writer

To store data in a field of a structure: (setf (<structure name> - <field name> <instance>) <data>) Example >(setf (student-last_name sn170345) 'meany) MEANY >(setf (student-first_name sn170345) '(sarah ann)) (SARAH ANN)

Predicate

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:
These are automatically created by defstruct and test to see if a symbol is a structure like this one. They act like symbolp, numberp, listp and other predicates. (<structure name> -p <a variable or symbol or list or whatever>) Example >(student-p sn170345) T >(student-p 'dog) NIL

Describe

(describe ) will return pertinent information about the name of the structure, the slot names and any default values.