100 lines
3 KiB
HTML
100 lines
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="node41.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
|
|
<A HREF="node29.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
|
|
<A HREF="node39.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="node41.html"> Programming Techniques</A>
|
|
<B>Up:</B>
|
|
<A HREF="node29.html"> Recursion and Iteration</A>
|
|
<B> Previous:</B>
|
|
<A HREF="node39.html"> Timing Function Calls</A>
|
|
<BR> <HR> <P>
|
|
<H1> Exercises</H1>
|
|
<P>
|
|
<DL COMPACT><DT>1.
|
|
<DD> Redefine power so that it can handle negative integer exponents (without using Lisp's own <code>expt</code> function).
|
|
<P>
|
|
<DT>2.
|
|
<DD> Write a function called MY-REMOVE which removes all occurrences of an element from a list. MY-REMOVE should use recursion. Here are a some examples of how your function should behave:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (my-remove 'hello '(hello why dont you say hello))
|
|
(WHY DONT YOU SAY)
|
|
> (my-remove '(oops my) '(1 2 (oops my) 4 5))
|
|
(1 2 4 5)
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
<DT>3.
|
|
<DD> Write an iterative version of the above and call it REMOVE-I.
|
|
<P>
|
|
<DT>4.
|
|
<DD> Lisp provides a predefined function MEMBER which behaves as follows:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (member 3 '(1 2 3 4 5) )
|
|
(3 4 5)
|
|
> (member 'hey '(whats going on here) )
|
|
NIL
|
|
> (member 'key '(where did (i (put) the (key))) )
|
|
NIL
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Write a recursive function called MY-MEMBER which checks for an atom inside nested lists. Here are examples:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (my-member 3 '(1 2 3 4 5) )
|
|
T
|
|
> (my-member 'hey '(whats going on here) )
|
|
NIL
|
|
> (my-member 'key '(where did (i (put) the (key))) )
|
|
T
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
<DT>5.
|
|
<DD> A palindrome is something that reads the same backwards as forwards. The following is a definition of PALINDROMEP which checks if a list is a palindrome.
|
|
<BLOCKQUOTE>
|
|
<PRE>(defun palindromep (lst)
|
|
(equal lst (reverse lst)) )
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
So, for example:
|
|
<BLOCKQUOTE>
|
|
<PRE>> (palindromep '(1 2 3 4 5 4 3 2 1))
|
|
T
|
|
> (palindromep '(a b b a))
|
|
T
|
|
> (palindromep '(1 2 3))
|
|
NIL
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Write a recursive version of this function called R-PALINDROMEP without using the function reverse.
|
|
<P>
|
|
<DT>6.
|
|
<DD> A mathematical expression in Lisp may look something like:
|
|
<BLOCKQUOTE>
|
|
<PRE> (+ (* 1 2 pi) 3 (- 4 5))
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
This would be more readable (to most humans) in ``infix'' notation:
|
|
<BLOCKQUOTE>
|
|
<PRE> ((1 * 2 * pi) + 3 + (4 - 5))
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
Write a function INFIX which given Lisp-like mathematical expressions, returns the infixed version.
|
|
<P>
|
|
</DL><BR> <HR>
|
|
<P>
|
|
<ADDRESS>
|
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
|
March 2007 </I>
|
|
</ADDRESS>
|
|
</BODY>
|