128 lines
3.8 KiB
HTML
128 lines
3.8 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="node29.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
|
|
<A HREF="node15.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
|
|
<A HREF="node27.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="node29.html"> Recursion and Iteration</A>
|
|
<B>Up:</B>
|
|
<A HREF="node15.html"> Defining Lisp functions</A>
|
|
<B> Previous:</B>
|
|
<A HREF="node27.html"> Logical Operators: And </A>
|
|
<BR> <HR> <P>
|
|
<H1> Exercises</H1>
|
|
<P>
|
|
<DL COMPACT><DT>1.
|
|
<DD> Use the Lisp interpreter to help you learn or refresh your memory about the behavior of these predicates:
|
|
<b>> </b>, <b><</b>, <b>> </b>=, <b><</b>=, =, zerop, numberp, symbolp, atom, constantp, listp, functionp
|
|
<P>
|
|
<DT>2.
|
|
<DD> Consider the following definition:
|
|
<BLOCKQUOTE>
|
|
<PRE>(defun life (a b)
|
|
(cond ((null a) b)
|
|
((null b) a)
|
|
(t 'its-tough)))
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Suppose you are running the Lisp interpreter and you enter the following:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (setf a 'oh-boy)
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Then you do the following:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (life 'gummi a)
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
What are the global and local values of a and b before, during, and after this command?
|
|
<P>
|
|
<DT>3.
|
|
<DD> Consider the following function definition:
|
|
<BLOCKQUOTE>
|
|
<PRE>(defun who-knows (lst1 lst2)
|
|
(cond ((= (length lst1) (length lst2))
|
|
(+ (length lst1) (length lst2)))
|
|
((> (length lst1) (length lst2)) (length lst2))
|
|
(t (length lst1))))
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
<DL COMPACT><DT>a.
|
|
<DD> What does this function do? Be precise as what would happen in each case.
|
|
<DT>b.
|
|
<DD> How would you make this function crash (return an ERROR)? Be careful in explaining why it will happen.
|
|
<P>
|
|
</DL>
|
|
<P>
|
|
<DT>4.
|
|
<DD> Write a function called BLENGTH (B stands for better) which is more tolerant of bad arguments, and is more informative. It works as follows:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (blength '(a b c d))
|
|
4
|
|
|
|
> (blength 'hello)
|
|
(sorry hello is an atom)
|
|
|
|
> (blength 4)
|
|
(sorry 4 is a number)
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Thus, if a list is passed in it should return the proper length, else if a number, or another type of atom is passed in, it should identify them as
|
|
such.
|
|
<P>
|
|
<DT>5.
|
|
<DD> Consider the following definition for the function CIRCULATE:
|
|
<BLOCKQUOTE>
|
|
<PRE>(defun circulate (lst)
|
|
(append (rest lst)
|
|
(list (first lst))))
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
This function takes a list and constructs a new list by taking the first element of the old list and making it the last element of the new. For
|
|
example:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (circulate '((whats) happening here))
|
|
(happening here (whats))
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Rewrite the function and call it CIRCULATE-DIR so that it can
|
|
circulate lists in both directions. Thus it should work as follows:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (circulate-dir '(1 2 3 4) 'left)
|
|
(4 1 2 3)
|
|
|
|
> (circulate-dir '(1 2 3 4) 'right)
|
|
(2 3 4 1)
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
<P>
|
|
<DT>6.
|
|
<DD> With the definition of CIRCULATE given above, what happens (and explain why) when we evaluate
|
|
<BLOCKQUOTE>
|
|
<PRE>a. (circulate 'hello)
|
|
|
|
b. (circulate nil)
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
<P>
|
|
<DT>7.
|
|
<DD> Define a function called MY-AND which acts like the Lisp AND function (but only takes 2 arguments) using only IF.
|
|
<P>
|
|
</DL><BR> <HR>
|
|
<P>
|
|
<ADDRESS>
|
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
|
March 2007 </I>
|
|
</ADDRESS>
|
|
</BODY>
|