emacs.d/clones/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node176.html
2022-08-26 19:11:35 +02:00

77 lines
4.4 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.1. Unnamed Structures</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Unnamed 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=tex2html3727 HREF="node177.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3725 HREF="node175.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3719 HREF="node175.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3729 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3730 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3728 HREF="node177.html"> Named Structures</A>
<B>Up:</B> <A NAME=tex2html3726 HREF="node175.html"> Structures of Explicitly </A>
<B> Previous:</B> <A NAME=tex2html3720 HREF="node175.html"> Structures of Explicitly </A>
<HR> <P>
<H2><A NAME=SECTION002371000000000000000>19.7.1. Unnamed Structures</A></H2>
<P>
Sometimes a particular data representation is imposed by external requirements,
and yet it is desirable to document the data format as a <tt>defstruct</tt>-style
structure. For example, consider expressions built up from numbers,
symbols, and binary operations such as <tt>+</tt> and <tt>*</tt>. An operation
might be represented as it is in Lisp, as a list of the operator
and the two operands. This fact can be expressed succinctly with <tt>defstruct</tt>
in this manner:
<P><pre>
(defstruct (binop (:type list))
(operator '? :type symbol)
operand-1
operand-2)
</pre><P>
This will define a constructor function <tt>make-binop</tt> and three
selector functions, namely <tt>binop-operator</tt>, <tt>binop-operand-1</tt>,
and <tt>binop-operand-2</tt>. (It will <i>not</i>, however, define a predicate
<tt>binop-p</tt>, for reasons explained below.)
<P>
The effect of <tt>make-binop</tt> is simply to construct a list of length 3:
<P><pre>
(make-binop :operator '+ :operand-1 'x :operand-2 5)
=> (+ x 5)
(make-binop :operand-2 4 :operator '*)
=> (* <tt>nil</tt> 4)
</pre><P>
It is just like the function <tt>list</tt> except that it takes
keyword arguments and performs slot defaulting appropriate to the <tt>binop</tt>
conceptual data type. Similarly, the selector functions
<tt>binop-operator</tt>, <tt>binop-operand-1</tt>,
and <tt>binop-operand-2</tt> are essentially equivalent to <tt>car</tt>,
<tt>cadr</tt>, and <tt>caddr</tt>, respectively. (They might not be
completely equivalent because,
for example, an implementation would be justified in adding error-checking
code to ensure that the argument to each selector function is a length-3
list.)
<P>
We speak of <tt>binop</tt> as being a ``conceptual'' data type because <tt>binop</tt>
is not made a part of the Common Lisp type system. The predicate
<tt>typep</tt> will not recognize <tt>binop</tt> as a type specifier, and <tt>type-of</tt>
will return <tt>list</tt> when given a <tt>binop</tt> structure. Indeed, there is
no way to distinguish a data structure constructed by <tt>make-binop</tt>
from any other list that happens to have the correct structure.
<P>
There is not even any way to recover the structure name <tt>binop</tt> from
a structure created by <tt>make-binop</tt>. This can be done, however,
if the structure is ``named.''
<P>
<BR> <HR><A NAME=tex2html3727 HREF="node177.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3725 HREF="node175.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3719 HREF="node175.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3729 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3730 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3728 HREF="node177.html"> Named Structures</A>
<B>Up:</B> <A NAME=tex2html3726 HREF="node175.html"> Structures of Explicitly </A>
<B> Previous:</B> <A NAME=tex2html3720 HREF="node175.html"> Structures of Explicitly </A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>