69 lines
2.7 KiB
HTML
69 lines
2.7 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> Constructors: Cons, List, and Append</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Constructors: Cons, List, and Append">
|
||
|
<meta name="keywords" value="lp">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<A HREF="node9.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="node7.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="node9.html"> Quote</A>
|
||
|
<B>Up:</B>
|
||
|
<A HREF="node7.html"> Some Primitive Functions</A>
|
||
|
<B> Previous:</B>
|
||
|
<A HREF="node7.html"> Some Primitive Functions</A>
|
||
|
<BR> <HR> <P>
|
||
|
<H2> Constructors: Cons, List, and Append</H2>
|
||
|
<P>
|
||
|
The primitive function ``cons'' allows you to add a member to the front of a list. Here are two examples:
|
||
|
<P>
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (cons 1 nil)
|
||
|
(1)
|
||
|
> (cons 1 (cons 2 nil))
|
||
|
(1 2)
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
It is worth looking at what is going on in these examples. In the first, cons is given two arguments. The first is 1 and the second is nil, which, you will remember, is the same as (). So, cons takes the first argument and inserts it as the first element of the second.
|
||
|
<P>
|
||
|
To understand the second example, it is useful to anthropomorphize the interpreter. It says, ``OK, cons is a function I know and I've
|
||
|
got two arguments to that function, 1 and (cons 2
|
||
|
nil). The first argument is an atom, but a special one, and
|
||
|
evaluates to 1. The second is a list, so its first element names a
|
||
|
function, i.e. cons, and there are two arguments to that function, 2
|
||
|
and nil. 2 evaluates to 2, and nil evaluates to nil (the empty
|
||
|
list). So, putting 2 into the empty list we get (2). Now I know
|
||
|
what the second argument to the first cons is, we can go ahead and
|
||
|
insert the first argument, i.e. 1. Hence we get (1 2).''
|
||
|
<P>
|
||
|
Now we discuss what happens when we deal with atoms that are not
|
||
|
special. Suppose you wanted to construct the list (a b c d) and you
|
||
|
try to do this by saying:
|
||
|
<P>
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>> (cons a (b c d))
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
What would happen? Well, the interpreter would say ``OK, cons I know,
|
||
|
and I've got two arguments to evaluate. First, eval a.''
|
||
|
<P>
|
||
|
Immediately you get an error because (we are assuming) a is an
|
||
|
unbound variable (you haven't set it to anything). How do you fix
|
||
|
this problem?
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<P>
|
||
|
<ADDRESS>
|
||
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
||
|
March 2007 </I>
|
||
|
</ADDRESS>
|
||
|
</BODY>
|