168 lines
8.1 KiB
HTML
168 lines
8.1 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>7.1.2. Assignment</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Assignment">
|
||
|
<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=tex2html2512 HREF="node80.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2510 HREF="node77.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2506 HREF="node78.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2514 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2515 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html2513 HREF="node80.html"> Generalized Variables</A>
|
||
|
<B>Up:</B> <A NAME=tex2html2511 HREF="node77.html"> Constants and Variables</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html2507 HREF="node78.html"> Reference</A>
|
||
|
<HR> <P>
|
||
|
<H2><A NAME=SECTION001112000000000000000>7.1.2. Assignment</A></H2>
|
||
|
<P>
|
||
|
The following facilities allow the value of a variable (more specifically,
|
||
|
the value associated with the current binding of the variable) to be altered.
|
||
|
Such alteration is different from establishing a new binding.
|
||
|
Constructs for establishing new bindings of variables are described
|
||
|
in section <A HREF="node83.html#VARBINDINGSECTION">7.5</A>.
|
||
|
<P>
|
||
|
<BR><b>[Special Form]</b><BR>
|
||
|
<tt>setq</tt> <tt>{<i>var</I></tt> <tt><i>form</I>}*</tt><P>The special form <tt>(setq <i>var1</i> <i>form1</i> <i>var2</i> <i>form2</i> ...)</tt> is the
|
||
|
``simple variable assignment statement'' of Lisp.
|
||
|
First <i>form1</i> is evaluated
|
||
|
and the result is stored in the variable <i>var1</i>, then <i>form2</i>
|
||
|
is evaluated and the result stored in <i>var2</i>, and so forth.
|
||
|
The variables are represented as symbols, of course, and are interpreted
|
||
|
as referring to static or dynamic instances according to the usual rules.
|
||
|
Therefore <tt>setq</tt> may be used for assignment of both lexical
|
||
|
and special variables.
|
||
|
<P>
|
||
|
<tt>setq</tt> returns the last value assigned, that is, the result of the
|
||
|
evaluation of its last argument.
|
||
|
As a boundary case, the form <tt>(setq)</tt> is legal and returns <tt>nil</tt>.
|
||
|
There must be an even number of argument forms.
|
||
|
For example, in
|
||
|
<P><pre>
|
||
|
(setq x (+ 3 2 1) y (cons x nil))
|
||
|
</pre><P>
|
||
|
<tt>x</tt> is set to <tt>6</tt>, <tt>y</tt> is set to <tt>(6)</tt>, and the <tt>setq</tt>
|
||
|
returns <tt>(6)</tt>. Note that the first assignment is performed before
|
||
|
the second form is evaluated, allowing that form to
|
||
|
use the new value of <tt>x</tt>.
|
||
|
<P>
|
||
|
See also the description of <tt>setf</tt>,
|
||
|
the Common Lisp ``general assignment statement'' that is capable of assigning
|
||
|
to variables, array elements, and other locations.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
Some programmers choose to avoid
|
||
|
<tt>setq</tt> as a matter of style, always using <tt>setf</tt> for any kind of
|
||
|
structure modification. Others use <tt>setq</tt> with simple variable names and
|
||
|
<tt>setf</tt> with all other generalized variables.
|
||
|
<P>
|
||
|
X3J13 voted in March 1989
|
||
|
(SYMBOL-MACROLET-SEMANTICS) <A NAME=5070> </A> to specify that if any <i>var</i>
|
||
|
refers not to an ordinary variable but to a binding made by
|
||
|
<tt>symbol-macrolet</tt>, then that <i>var</i> is handled as
|
||
|
if <tt>setf</tt> had been used instead of <tt>setq</tt>.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<BR><b>[Macro]</b><BR>
|
||
|
<tt>psetq</tt> <tt>{<i>var</I></tt> <tt><i>form</I>}*</tt><P>A <tt>psetq</tt> form is just like a <tt>setq</tt> form, except
|
||
|
that the assignments happen in parallel. First all of the forms
|
||
|
are evaluated, and then the variables are set to the resulting
|
||
|
values. The value of the <tt>psetq</tt> form is <tt>nil</tt>.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(setq a 1)
|
||
|
(setq b 2)
|
||
|
(psetq a b b a)
|
||
|
a => 2
|
||
|
b => 1
|
||
|
</pre><P>
|
||
|
In this example, the values of <tt>a</tt> and <tt>b</tt> are exchanged by
|
||
|
using parallel assignment.
|
||
|
(If several variables are to be assigned in parallel in
|
||
|
the context of a loop, the <tt>do</tt> construct may be appropriate.)
|
||
|
<P>
|
||
|
See also the description of <tt>psetf</tt>,
|
||
|
the Common Lisp ``general parallel assignment statement'' that
|
||
|
is capable of assigning
|
||
|
to variables, array elements, and other locations.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in March 1989
|
||
|
(SYMBOL-MACROLET-SEMANTICS) <A NAME=5091> </A> to specify that if any <i>var</i>
|
||
|
refers not to an ordinary variable but to a binding made by
|
||
|
<tt>symbol-macrolet</tt>, then that <i>var</i> is handled as
|
||
|
if <tt>psetf</tt> had been used instead of <tt>psetq</tt>.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>set</tt> <tt><i>symbol</I></tt> <tt><i>value</I></tt><P><tt>set</tt> allows alteration of the value of a dynamic (special) variable.
|
||
|
<tt>set</tt> causes the dynamic variable named by <i>symbol</i> to take on
|
||
|
<i>value</i> as its value.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in January 1989
|
||
|
(ARGUMENTS-UNDERSPECIFIED) <A NAME=5105> </A>
|
||
|
to clarify that the <i>value</i>
|
||
|
may be any Lisp datum whatsoever.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
Only the value of the current dynamic binding is altered;
|
||
|
if there are no bindings in effect, the most global value is altered.
|
||
|
For example,
|
||
|
<P><pre>
|
||
|
(set (if (eq a b) 'c 'd) 'foo)
|
||
|
</pre><P>
|
||
|
will either set <tt>c</tt> to <tt>foo</tt> or set <tt>d</tt> to <tt>foo</tt>, depending
|
||
|
on the outcome of the test <tt>(eq a b)</tt>.
|
||
|
<P>
|
||
|
<tt>set</tt> returns <i>value</i> as its result.
|
||
|
<P>
|
||
|
<tt>set</tt> cannot alter
|
||
|
the value of a local (lexically bound) variable.
|
||
|
The special form <tt>setq</tt>
|
||
|
is usually used for altering the values of variables
|
||
|
(lexical or dynamic) in programs.
|
||
|
<tt>set</tt> is particularly useful for implementing interpreters for
|
||
|
languages embedded in Lisp.
|
||
|
See also <tt>progv</tt>, a construct that performs binding rather
|
||
|
than assignment of dynamic variables.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>makunbound</tt> <tt><i>symbol</I></tt> <tt><br></tt><tt>fmakunbound</tt> <tt><i>symbol</I></tt><P><tt>makunbound</tt> causes the dynamic (special) variable named
|
||
|
by <i>symbol</i> to become unbound (have no value). <tt>fmakunbound</tt>
|
||
|
does the analogous thing for the global function definition named
|
||
|
by <i>symbol</i>.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(setq a 1)
|
||
|
a => 1
|
||
|
(makunbound 'a)
|
||
|
a => causes an error
|
||
|
|
||
|
(defun foo (x) (+ x 1))
|
||
|
(foo 4) => 5
|
||
|
(fmakunbound 'foo)
|
||
|
(foo 4) => causes an error
|
||
|
</pre><P>
|
||
|
Both functions return <i>symbol</i> as the result value.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in March 1989 (FUNCTION-NAME) <A NAME=5133> </A> to extend <tt>fmakunbound</tt>
|
||
|
to accept any function-name (a symbol or a list
|
||
|
whose <i>car</i> is <tt>setf</tt> - see section <A HREF="node77.html#FUNCTIONNAMESECTION">7.1</A>).
|
||
|
Thus one may write <tt>(fmakunbound '(setf cadr))</tt> to remove any
|
||
|
global definition of a <tt>setf</tt> expansion function for <tt>cadr</tt>.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<BR> <HR><A NAME=tex2html2512 HREF="node80.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2510 HREF="node77.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2506 HREF="node78.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2514 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2515 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html2513 HREF="node80.html"> Generalized Variables</A>
|
||
|
<B>Up:</B> <A NAME=tex2html2511 HREF="node77.html"> Constants and Variables</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html2507 HREF="node78.html"> Reference</A>
|
||
|
<HR> <P>
|
||
|
<HR>
|
||
|
<P><ADDRESS>
|
||
|
AI.Repository@cs.cmu.edu
|
||
|
</ADDRESS>
|
||
|
</BODY>
|