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

119 lines
4.6 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> Quote</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Quote">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node10.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node7.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node8.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="node10.html"> Selectors: First and </A>
<B>Up:</B>
<A HREF="node7.html"> Some Primitive Functions</A>
<B> Previous:</B>
<A HREF="node8.html"> Constructors: ConsList, </A>
<BR> <HR> <P>
<H2> Quote</H2>
<P>
The answer to the previous question is to use the single quote mark, ', in front of an item that you do not wish the interpreter to evaluate. It works
as follows:
<P>
<BLOCKQUOTE>
<PRE>&gt; 'a
a
</PRE>
</BLOCKQUOTE>
Without the quote you would have received an error message. So now
you might try (cons 'a (b c d)), but this still won't work since the
interpreter tries to evaluate the second argument (b c d), treating b
as the name of a function. But (we are assuming) you have not
defined b to be a function, so it is an error. Once again you can
use the ' to block evaluation of the list (b c d). Thus:
<P>
<BLOCKQUOTE>
<PRE>&gt; (cons 'a '(b c d))
(a b c d)
</PRE>
</BLOCKQUOTE>
Notice that you need only one quote on the second argument. The
quote blocks evaluation of the whole thing, so you don't need quotes
on the internal elements of the list.
<P>
Notice, also, that in using setq, the first argument -- the variable to
be set -- does not need a quote; the interpreter does not evaluate it
if it is an atom. Originally, Lisp had only the function set, whose
proper use included things like (set 'a 4) or (set 'a 'b). Lisp
programmers got tired of putting (or, more likely, forgetting to put)
the quote on the first argument, so they wrote setq to take care of
it automatically (now you understand the name, right?!) So, (setq a
4) is equivalent to (set 'a 4).
<P>
In fact, the ' itself is shorthand for another function. The interpreter expands 'a to the expression (quote a). So (setq a 4) is really
short for (set (quote a) 4). Shorthand commands like setq and ' are
called ``macros'' by programmers.
<P>
Cons always takes two arguments. (Try it with one or three and see
that you get an error message.) Usually, the second argument is a
list, but the first can be either an atom or a list. (Cons can be
used with an atom as the second argument. What gets returned is a
slightly esoteric data type called a ``dotted pair.'' The use of
dotted pairs is relatively rare and will be ignored here.)
<P>
The list building functions list and append both allow arbitrary
numbers of arguments. List takes all its arguments and makes a list
with them as elements. Arguments to list will typically be either
atoms or lists. For example:
<P>
<BLOCKQUOTE>
<PRE>&gt; (list 2 3 4)
(2 3 4)
&gt; (list 'a '(a s d f))
(a (a s d f))
</PRE>
</BLOCKQUOTE>
Make sure you understand why list works as shown in these examples.
<P>
Append is normally used with lists as arguments. (Only the last
argument may be an atom, resulting in a dotted pair.) When used with
lists, append has the effect of taking all the members of the lists
that are its arguments and creating a new list from those elements.
For example:
<P>
<BLOCKQUOTE>
<PRE>&gt; (append '(a b) '(c d))
(a b c d)
</PRE>
</BLOCKQUOTE>
Beginning Lisp programmers (and even some experienced ones)
frequently have trouble deciding whether they should use cons or list
or append to achieve a particular effect. The best way to get around
the difficulty is to play with the different functions and see what
works.
<P>
<BR> <HR>
<A HREF="node10.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node7.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node8.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="node10.html"> Selectors: First and </A>
<B>Up:</B>
<A HREF="node7.html"> Some Primitive Functions</A>
<B> Previous:</B>
<A HREF="node8.html"> Constructors: ConsList, </A>
<BR> <HR> <P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>