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

76 lines
2.2 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</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Setf">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node14.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node2.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node12.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="node14.html"> Exercises</A>
<B>Up:</B>
<A HREF="node2.html"> LISt Processing</A>
<B> Previous:</B>
<A HREF="node12.html"> More Functions and </A>
<BR> <HR> <P>
<H1> Setf</H1>
<P>
Setq is useful for changing the values of variables. For example:
<BLOCKQUOTE>
<PRE>&gt; (setq my-age (+ my-age 1))
11
&gt; (setq a (cdr a))
(a s d f)
</PRE>
</BLOCKQUOTE>
Lisp programs very frequently make use of changes of this sort. But
sometimes one would like to change just part of the value of a
variable. Suppose you assign a value to a variable as follows:
<BLOCKQUOTE>
<PRE>&gt; (setq words '(a list of words))
(A LIST OF WORDS)
</PRE>
</BLOCKQUOTE>
What if you want to change just part of the list that is the value of
words? Well, you could say
<BLOCKQUOTE>
<PRE>&gt; (setq words (cons 'this (rest words)))
(THIS LIST OF WORDS)
</PRE>
</BLOCKQUOTE>
but with lengthy list structures this can get complicated. What you
need is a way to change just part of a list; setf is what you need.
Look at this sequence to see just some of the ways in which it can be
used.
<BLOCKQUOTE>
<PRE>&gt; (setf (first words) 'the)
THE
&gt; words
(THE LIST OF WORDS)
&gt; (setf (third words) 'is)
IS
&gt; words
(THE LIST IS WORDS)
&gt; (setf (rest words) '(game is up))
(GAME IS UP)
&gt; words
(THE GAME IS UP)
</PRE>
</BLOCKQUOTE>
Now you know enough to do the exercises below.
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>