Structures (Structs) in Lisp
Example: (defstruct person
(sex nil) ;;Field #1 Default value is nil
(personality 'nice) ;;Field #2 Default value is nice
)
Template for a struct:
(defstruct <structure name>
(<field name 1> <default value 1>)
(<field name 2> <default value 2>)
. . .
(<field name n> <default value n>)
DEFSTRUCT does this:
- Creates a constructor procedure: (make-<structure name>)
Ex: (setf p1 (make-person))
- Creates reader procedures for getting information out of an
instance's fields:
(<structure name>-<field name> <instance name>)
Ex: (person-personality p1) returns NICE
- Generalizes SETF to serve as a writer when used with the readers
(setf (<structure name>-<field name> <instance name>) <value>)
Ex: (setf (person-sex p1) 'female)
- Creates a predicate for testing objects to see if they are
instances of the defined data type:
(<structure name>-p <instance name>)
Ex: (person-p p1) returns T
(person-p 'bobby) returns NIL
DESCRIBE will print descriptions of the defined structure.