emacs.d/clones/lisp/colinallen.dnsalias.org/lp/node56.html

146 lines
5.4 KiB
HTML
Raw Normal View History

2022-08-02 12:34:59 +02:00
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Originally converted to HTML using LaTeX2HTML 95 (Thu Jan 19 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE> Defstruct</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Defstruct">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node57.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node50.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node55.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node57.html"> Exercises.</A>
<B>Up:</B>
<A HREF="node50.html"> Simple Data Structures </A>
<B> Previous:</B>
<A HREF="node55.html"> Strings</A>
<BR> <HR> <P>
<H1> Defstruct</H1>
<P>
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:
<P>
<BLOCKQUOTE>
<PRE>&gt; (defstruct employee
age
first-name
last-name
sex
children)
EMPLOYEE
</PRE>
</BLOCKQUOTE>
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.
<P>
As with the other data types before, it is useful to associate particular instances with a symbol for easy access:
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf employee1 (make-employee))
#S(EMPLOYEE AGE NIL FIRST-NAME NIL LAST-NAME NIL SEX NIL
CHILDREN NIL)
</PRE>
</BLOCKQUOTE>
(Different implementations of Lisp will display structures in different ways.)
<P>
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:
<P>
<BLOCKQUOTE>
<PRE>&gt; (employee-age employee1)
NIL
&gt; (employee-sex employee1)
NIL
</PRE>
</BLOCKQUOTE>
Slot values can be assigned using setf:
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf (employee-age employee1) 56)
56
&gt; (employee-age employee1)
56
</PRE>
</BLOCKQUOTE>
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:
<P>
<BLOCKQUOTE>
<PRE>&gt; (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)
&gt; (employee-first-name employee2)
ALICE
</PRE>
</BLOCKQUOTE>
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.
<P>
Defstruct also allows you to specify default values for given slots. Here is an example:
<P>
<BLOCKQUOTE>
<PRE>&gt; (defstruct trekkie
(sex 'male)
(intelligence 'high)
age)
TREKKIE
</PRE>
</BLOCKQUOTE>
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:
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf t1 (make-trekkie))
#S(TREKKIE SEX MALE INTELLIGENCE HIGH AGE NIL)
&gt; (setf t2 (make-trekkie :age 35))
#S(TREKKIE SEX MALE INTELLIGENCE HIGH AGE 35)
&gt; (setf t3 (make-trekkie :age 28 :sex 'female))
#S(TREKKIE SEX FEMALE INTELLIGENCE HIGH AGE 28)
</PRE>
</BLOCKQUOTE>
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:
<P>
<BLOCKQUOTE>
<PRE>&gt; (typep t1 'employee)
NIL
&gt; (typep t1 'trekkie)
T
&gt; (trekkie-p t1)
T
&gt; (employee-p t3)
NIL
</PRE>
</BLOCKQUOTE>
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 <a href="http://www.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/cltl2.html"><em>Common Lisp the language</em></a>.
<P>
<BR> <HR>
<A HREF="node57.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node50.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node55.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <BR>
<A HREF="lp.html"><B>Contents</B></A>
<B> Next:</B>
<A HREF="node57.html"> Exercises.</A>
<B>Up:</B>
<A HREF="node50.html"> Simple Data Structures </A>
<B> Previous:</B>
<A HREF="node55.html"> Strings</A>
<BR> <HR> <P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>