emacs.d/clones/colinallen.dnsalias.org/lp/node40.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>&gt; (my-remove 'hello '(hello why dont you say hello))
(WHY DONT YOU SAY)
&gt; (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>&gt; (member 3 '(1 2 3 4 5) )
(3 4 5)
&gt; (member 'hey '(whats going on here) )
NIL
&gt; (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>&gt; (my-member 3 '(1 2 3 4 5) )
T
&gt; (my-member 'hey '(whats going on here) )
NIL
&gt; (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>&gt; (palindromep '(1 2 3 4 5 4 3 2 1))
T
&gt; (palindromep '(a b b a))
T
&gt; (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>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>