91 lines
4.9 KiB
HTML
91 lines
4.9 KiB
HTML
![]() |
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
||
|
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
||
|
<HEAD>
|
||
|
<TITLE>19.7.2. Named Structures</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Named Structures">
|
||
|
<meta name="keywords" value="clm">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<b>Common Lisp the Language, 2nd Edition</b>
|
||
|
<BR> <HR><A NAME=tex2html3739 HREF="node178.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3737 HREF="node175.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3731 HREF="node176.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3741 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3742 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html3740 HREF="node178.html"> Other Aspects of </A>
|
||
|
<B>Up:</B> <A NAME=tex2html3738 HREF="node175.html"> Structures of Explicitly </A>
|
||
|
<B> Previous:</B> <A NAME=tex2html3732 HREF="node176.html"> Unnamed Structures</A>
|
||
|
<HR> <P>
|
||
|
<H2><A NAME=SECTION002372000000000000000>19.7.2. Named Structures</A></H2>
|
||
|
<P>
|
||
|
A ``named'' structure has the property that, given an instance of the
|
||
|
structure, the structure name (that names the type) can be reliably
|
||
|
recovered. For structures defined
|
||
|
with no <tt>:type</tt> option, the structure name actually becomes part
|
||
|
of the Common Lisp data-type system. The function <tt>type-of</tt>,
|
||
|
when applied to such a structure, will return the structure name
|
||
|
as the type of the object; the predicate <tt>typep</tt> will recognize
|
||
|
the structure name as a valid type specifier.
|
||
|
<P>
|
||
|
For structures defined with a <tt>:type</tt> option, <tt>type-of</tt> will
|
||
|
return a type specifier such as <tt>list</tt> or <tt>(vector t)</tt>,
|
||
|
depending on the type specified to the <tt>:type</tt> option.
|
||
|
The structure name does not become a valid type specifier.
|
||
|
However,
|
||
|
if the <tt>:named</tt> option is also specified, then the first component
|
||
|
of the structure (as created by a <tt>defstruct</tt> constructor function)
|
||
|
will always contain the structure name. This allows the structure name
|
||
|
to be recovered from an instance of the structure and allows a reasonable
|
||
|
predicate for the conceptual type to be defined:
|
||
|
the automatically defined
|
||
|
<tt><i>name</i>-p</tt> predicate for the structure operates by first
|
||
|
checking that its argument is of the proper type (<tt>list</tt>, <tt>(vector t)</tt>,
|
||
|
or whatever) and then checking whether the first component contains
|
||
|
the appropriate type name.
|
||
|
<P>
|
||
|
Consider the <tt>binop</tt> example shown above, modified only to
|
||
|
include the <tt>:named</tt> option:
|
||
|
<P><pre>
|
||
|
(defstruct (binop (:type list) :named)
|
||
|
(operator '? :type symbol)
|
||
|
operand-1
|
||
|
operand-2)
|
||
|
</pre><P>
|
||
|
As before, this will define a constructor function <tt>make-binop</tt> and three
|
||
|
selector functions <tt>binop-operator</tt>, <tt>binop-operand-1</tt>,
|
||
|
and <tt>binop-operand-2</tt>. It will also define a predicate <tt>binop-p</tt>.
|
||
|
<P>
|
||
|
The effect of <tt>make-binop</tt> is now to construct a list of length 4:
|
||
|
<P><pre>
|
||
|
(make-binop :operator '+ :operand-1 'x :operand-2 5)
|
||
|
=> (binop + x 5)
|
||
|
|
||
|
(make-binop :operand-2 4 :operator '*)
|
||
|
=> (binop * <tt>nil</tt> 4)
|
||
|
</pre><P>
|
||
|
The structure has the same layout as before except that the structure name
|
||
|
<tt>binop</tt> is included as the first list element.
|
||
|
The selector functions
|
||
|
<tt>binop-operator</tt>, <tt>binop-operand-1</tt>,
|
||
|
and <tt>binop-operand-2</tt> are essentially equivalent to <tt>cadr</tt>,
|
||
|
<tt>caddr</tt>, and <tt>cadddr</tt>, respectively.
|
||
|
The predicate <tt>binop-p</tt> is more or less equivalent to the following
|
||
|
definition.
|
||
|
<P><pre>
|
||
|
(defun binop-p (x)
|
||
|
(and (consp x) (eq (car x) 'binop)))
|
||
|
</pre><P>
|
||
|
The name <tt>binop</tt> is still not a valid type specifier recognizable
|
||
|
to <tt>typep</tt>, but at least there is a way of distinguishing <tt>binop</tt>
|
||
|
structures from other similarly defined structures.
|
||
|
<P>
|
||
|
<br><HR><A NAME=tex2html3739 HREF="node178.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3737 HREF="node175.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3731 HREF="node176.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3741 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3742 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html3740 HREF="node178.html"> Other Aspects of </A>
|
||
|
<B>Up:</B> <A NAME=tex2html3738 HREF="node175.html"> Structures of Explicitly </A>
|
||
|
<B> Previous:</B> <A NAME=tex2html3732 HREF="node176.html"> Unnamed Structures</A>
|
||
|
<HR> <P>
|
||
|
<HR>
|
||
|
<P><ADDRESS>
|
||
|
AI.Repository@cs.cmu.edu
|
||
|
</ADDRESS>
|
||
|
</BODY>
|