emacs.d/clones/colinallen.dnsalias.org/lp/node113.html

97 lines
3 KiB
HTML

<!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> setf (MACRO)</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" setf (MACRO)">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node114.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node72.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node112.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="node114.html"> symbolp (PREDICATE)</A>
<B>Up:</B>
<A HREF="node72.html"> Appendix: Selected Lisp </A>
<B> Previous:</B>
<A HREF="node112.html"> secondthird, etc. </A>
<BR> <HR> <P>
<H1> setf (MACRO)</H1>
<P>
<b> Format:</b>
<BLOCKQUOTE>
<PRE>(setf &lt;place1&gt; &lt;val1&gt;
&lt;place2&gt; &lt;val2&gt;
.
.
&lt;placeN&gt; &lt;valN&gt; )
</PRE>
</BLOCKQUOTE>
<P>
<b> Required arguments:</b> none
<P>
<b> Optional arguments:</b> any even number of arguments
<P>
<tt> &lt;place&gt; </tt>: either (i) the name of a variable, or (ii) an expression referring to part of a larger structure (e.g. a list, property list, structure, or array).
<tt> &lt;val&gt; </tt>: any Lisp expression.
<P>
setf assigns the result of evaluating <tt> &lt;val&gt; </tt> to the location
specified in the immediately preceding <tt> &lt;place&gt; </tt>. It returns the
result of evaluating the last <tt> &lt;val&gt; </tt>. If no <tt> &lt;place&gt; -&lt;val&gt; </tt>
pairs are specified, setf returns nil. setf is used, among other
things, to assign values to variables, change parts of list structures,
and to manage property lists and structures. Examples of all these uses
are given in the chapters of this book. Other uses, too numerous to document
here, can be found in Steele.
<P>
<b> Examples:</b> (see all chapters for further examples)
<P>
<BLOCKQUOTE>
<PRE>&gt; (setf crew '(picard riker worf crusher))
(PICARD RIKER WORF CRUSHER)
&gt; (setf (first crew) (list 'captain (first crew))
(second crew) (list 'commander (second crew))
(third crew) (list 'lieutenant (third crew))
(fourth crew) (list 'ensign (fourth crew)))
(ENSIGN CRUSHER)
&gt; crew
((CAPTAIN PICARD) (COMMANDER RIKER) (LIEUTENANT WORF)
(ENSIGN CRUSHER))
&gt; (setf (get 'picard 'rank) 'captain)
CAPTAIN
&gt; (get 'picard 'rank)
CAPTAIN
&gt; (defstruct starship crew captain)
STARSHIP
&gt; (setf enterprise (make-starship))
#S(STARSHIP CREW NIL CAPTAIN NIL)
&gt; (setf (starship-crew enterprise) (rest crew)
(starship-captain enterprise) (second (first crew)))
PICARD
&gt; enterprise
#S(STARSHIP CREW
((COMMANDER RIKER) (LIEUTENANT WORF) (ENSIGN CRUSHER))
CAPTAIN PICARD)
</PRE>
</BLOCKQUOTE>
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>