121 lines
3.3 KiB
HTML
121 lines
3.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> Exercises</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Exercises">
|
||
|
<meta name="keywords" value="lp">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<BR> <HR>
|
||
|
<A HREF="node15.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="node13.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="node15.html"> Defining Lisp functions</A>
|
||
|
<B>Up:</B>
|
||
|
<A HREF="node2.html"> LISt Processing</A>
|
||
|
<B> Previous:</B>
|
||
|
<A HREF="node13.html"> Setf</A>
|
||
|
<BR> <HR> <P>
|
||
|
<H1> Exercises</H1>
|
||
|
<P>
|
||
|
Note: A temptation if you are not used to computers is to sit down
|
||
|
and try to work out these exercises in your head and be satisfied
|
||
|
when you have reached some answer or other. <em> DON'T!</em> Use the Lisp
|
||
|
interpreter to check your understanding. If you don't understand why
|
||
|
the interpreter responds in some way, try to figure it out by playing
|
||
|
with some slight variations of the problems. Some of the examples
|
||
|
below (intentionally) generate interpreter errors. In such cases,
|
||
|
think about why the error occurs and how you might change things to
|
||
|
eliminate it.
|
||
|
<P>
|
||
|
<DL COMPACT><DT>1.
|
||
|
<DD>Evaluate the following:
|
||
|
<DL COMPACT><DT>a.
|
||
|
<DD>(first '(((a)) (b c d e)))
|
||
|
<DT>b.
|
||
|
<DD>(rest '(((((f))))))
|
||
|
<DT>c.
|
||
|
<DD>(first '(rest (a b c)))
|
||
|
<DT>d.
|
||
|
<DD>(first '(rest (rest (a b c))))
|
||
|
<DT>e.
|
||
|
<DD>(cons '(my life as) '(a dog))
|
||
|
<DT>f.
|
||
|
<DD>(append '(my life as) '(a dog))
|
||
|
<DT>g.
|
||
|
<DD>(list '(my life as) '(a dog))
|
||
|
<DT>h.
|
||
|
<DD>(cons (rest nil) (first nil))
|
||
|
<DT>i.
|
||
|
<DD>(abs (- (length (rest '(((a b) (c d))))) 5))
|
||
|
<DT>j.
|
||
|
<DD>(reverse (cons '(rest (reverse '(its gut na mur ta give captin)))))
|
||
|
<P>
|
||
|
</DL>
|
||
|
<P>
|
||
|
<DT>2.
|
||
|
<DD>Using first and rest extract the atom ``jim'' from the following:
|
||
|
<DL COMPACT><DT>a.
|
||
|
<DD>(he is dead jim)
|
||
|
<DT>b.
|
||
|
<DD>(captain (((jim) kirk)))
|
||
|
<DT>c.
|
||
|
<DD>(((((spock) asked) jim) if) he was all right)
|
||
|
<DT>d.
|
||
|
<DD>(after (looking at the (lizard man) ((((jim))) asked
|
||
|
for warp 9)))
|
||
|
<P>
|
||
|
</DL>
|
||
|
<DT>3.
|
||
|
<DD> What is returned by each of the following expressions (assume
|
||
|
they are evaluated in the given order)?
|
||
|
<DL COMPACT><DT>a.
|
||
|
<DD>(setf trek '(picard riker laforge worf))
|
||
|
<DT>b.
|
||
|
<DD>(cons 'data trek)
|
||
|
<DT>c.
|
||
|
<DD>trek
|
||
|
<DT>d.
|
||
|
<DD>(length (cons 'troi trek))
|
||
|
<DT>e.
|
||
|
<DD>(setf trek (cons 'data trek))
|
||
|
<DT>f.
|
||
|
<DD>(length (cons 'troi trek))
|
||
|
<P>
|
||
|
</DL>
|
||
|
<DT>4.
|
||
|
<DD>Given the following definition:
|
||
|
<BLOCKQUOTE>
|
||
|
<PRE>(setf mylist '((bush broccoli) (nixon watergate)
|
||
|
(letterman (viewer mail))
|
||
|
(you are no jack kennedy)
|
||
|
(and please) (scorsese (robert deniro))))
|
||
|
</PRE>
|
||
|
</BLOCKQUOTE>
|
||
|
Construct the following with any of the functions you have learned so
|
||
|
far.
|
||
|
<DL COMPACT><DT>a.
|
||
|
<DD>(no broccoli please)
|
||
|
<DT>b.
|
||
|
<DD>((scorsese and deniro) are no robert kennedy)
|
||
|
<DT>c.
|
||
|
<DD>(watergate and no viewer)
|
||
|
<DT>d.
|
||
|
<DD>(bush nixon kennedy)
|
||
|
<DT>e.
|
||
|
<DD>((bush broccoli) (nixon watergate) (letterman mail))
|
||
|
<P>
|
||
|
</DL>
|
||
|
</DL><BR> <HR>
|
||
|
<P>
|
||
|
<ADDRESS>
|
||
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
||
|
March 2007 </I>
|
||
|
</ADDRESS>
|
||
|
</BODY>
|