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

72 lines
3 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> Property Lists</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Property Lists">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node53.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="node51.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="node53.html"> ArraysVectors, and </A>
<B>Up:</B>
<A HREF="node50.html"> Simple Data Structures </A>
<B> Previous:</B>
<A HREF="node51.html"> Association Lists</A>
<BR> <HR> <P>
<H1> Property Lists</H1>
<P>
An alternative way to attach data to symbols is to use Common Lisp's property list feature. For each symbol, the Lisp interpreter maintains a list of properties which can be accessed with the function get.
<P>
Get expects to be given a symbol and a key. If a value has been set for that key, it is returned, otherwise get returns nil.
<P>
<BLOCKQUOTE>
<PRE>&gt; (get 'mary 'age)
NIL
&gt; (setf (get 'mary 'age) 45)
45
&gt; (get 'mary 'age)
45
</PRE>
</BLOCKQUOTE>
As this example shows, the value to be returned by a get expression is set using setf. (This is another place where setf works but setq will not.) The way to think of setf's behavior here is that you tell it exactly what you will type and what should be returned. The example here assigns the value 45 as the value to be returned when you type (get 'mary 'age).
<P>
Additional properties can be added in the same way.
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf (get 'mary 'job) 'banker)
BANKER
&gt; (setf (get 'mary 'sex) 'female)
FEMALE
&gt; (setf (get 'mary 'children) '(bonnie bob))
(BONNIE BOB)
</PRE>
</BLOCKQUOTE>
If, for some reason, you need to see all the properties a symbol has, you can do so:
<P>
<BLOCKQUOTE>
<PRE>&gt; (symbol-plist 'mary)
(SEX FEMALE JOB BANKER AGE 45 CHILDREN (BONNIE BOB))
</PRE>
</BLOCKQUOTE>
Get is a convenient way to manage related data, and in some ways is more flexible than assoc. For many applications, however, it is necessary to build more sophisticated data structures. Options for doing this include customized nested list structures, arrays, and use of the Common Lisp Object System (CLOS). Arrays, vectors, and strings will be introduced in the rest of this chapter. Customized list structures will not be covered here because they are highly dependent on the specific application. CLOS will not be covered here, either, as the purpose of this book is to bring you to a point where you can easily understand other presentations of advanced Lisp topics.
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>