emacs.d/clones/colinallen.dnsalias.org/lp/node45.html

209 lines
6.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> Recursion on Numbers</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Recursion on Numbers">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node46.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node41.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node44.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="node46.html"> Ensuring Proper Termination</A>
<B>Up:</B>
<A HREF="node41.html"> Programming Techniques</A>
<B> Previous:</B>
<A HREF="node44.html"> Recursion on Nested </A>
<BR> <HR> <P>
<H1> Recursion on Numbers</H1>
<P>
There are some functions which recur on a list of elements, but which
return a number. The function ``length'' defined earlier is such a
function. It takes an arbitrarily long list of elements and returns a
count of its top-level elements. In the case of length, we are
building a number by additions; at each level we add a one. One can
also imagine a function which builds a number with consecutive
multiplications. In both cases one must be careful in choosing which
value to return at the terminating line.
<BLOCKQUOTE>
<PRE><TT><H4><b>RULE OF THUMB 5:
<P>
When building a number value using +, return 0 at the
<P>
terminating line. When building a number value using
<P>
*, return 1 at the terminating line.
<P>
</b></h4></TT></PRE>
</BLOCKQUOTE>
<P>
<BLOCKQUOTE>
<PRE><TT><H4><b>Example 5:
<P>
Write a function, ``sum-of-list,'' which takes a list of numbers and returns
<P>
their sum. Write a similar function, ``product-of-list,'' which takes a
<P>
list of numbers and returns their product.
<P>
</b></h4></TT></PRE>
</BLOCKQUOTE>
Using the ideas presented in previous sections, writing these functions
should be simple. Again, we need to do three things: first we will
check for the end of the list; second we will use the first element of
the list in the addition (or multiplication); lastly the number
will be added (or multipled) to the recursive call on the ``rest'' of
the list. According to Rule of Thumb 5, we must remember that at the
terminating line we must return 0 for addition and 1 for
multiplication.
<P>
The following are the proposed solutions:
<BLOCKQUOTE>
<PRE> (defun sum-of-list (lst)
(cond ((null lst) 0)
(t (+ (first lst)
(sum-of-list (rest lst))))))
(defun product-of-list (lst)
(cond ((null lst) 1)
(t (* (first lst)
(product-of-list (rest lst))))))
</PRE>
</BLOCKQUOTE>
In the above examples, although we are building a number, we are still
recurring on a list. It is also possible to recur on a number. The
structure of recursion on numbers is very similar to that on simple lists.
<BLOCKQUOTE>
<PRE><TT> <H4><b>RULE OF THUMB 6:
<P>
When recurring on a number, do three things:
<P>
1. check for the termination condition;
<P>
2. use the number in some form;
<P>
3. recur with a changed form of the number.
<P>
</b></h4></TT></PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE><TT> <H4><b>Example 6:
<P>
The factorial of a non-negative integer, n, is
<P>
n * (n-1) * (n-2) * ... * 3 * 2 * 1.
<P>
Also, the factorial of 0 is 1. Implement the factorial function
<P>
in Lisp.
<P>
</b></h4></TT></PRE>
</BLOCKQUOTE>
The above problem is naturally recursive. In fact, it is very often
represented mathematically as the following recurrence relation:
<pre><tt>
factorial(n) = {n * factorial(n-1) if n&gt; 0}
{1 if n=0}
</tt></pre>
This translates easily into the following Lisp function definition:
<BLOCKQUOTE>
<PRE> (defun factorial (n)
(cond ((= n 0) 1)
(t (* n (factorial (- n 1))))))
</PRE>
</BLOCKQUOTE>
Let us try to identify the three element of Rule of Thumb 6:
<DL COMPACT><DT>(1)
<DD> We check for termination by testing if n has been reduced to 0.
<DT>(2)
<DD> We use the number, n, in the multiplication.
<DT>(3)
<DD> We do recur on a changed form of the number, i.e. on n
decremented by one.
<P>
</DL>
The following notation gives an idea of the execution of ``factorial'':
<BLOCKQUOTE>
<PRE>(factorial 4)
= (* 4 (factorial 3))
= (* 4 (* 3 (factorial 2)))
= (* 4 (* 3 (* 2 (factorial 1))))
= (* 4 (* 3 (* 2 (* 1 (factorial 0)))))
= (* 4 (* 3 (* 2 (* 1 1))))
= (* 4 (* 3 (* 2 1)))
= (* 4 (* 3 2))
= (* 4 6)
= 24
</PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE><TT> <H4><b>Example 7:
<P>
Write a function called ``remainder,'' which takes two positive non-zero
<P>
numbers, n and m, and returns the remainder when n is divided by m.
<P>
</b></h4></TT></PRE>
</BLOCKQUOTE>
<P>
Our strategy will be to repeatedly subtract m from n till n is less
than m; at this point n will be the value of the remainder. Let us
proceed in the steps suggested by Rule of Thumb 6:
<P>
<DL COMPACT><DT>(1)
<DD> We know that we can stop when <b>n &lt; m</b>. This will be our termination
condition and we will return the value of n.
<DT>(2)
<DD> At each level of recursion we will subtract m from n; this
represents our use of n.
<DT>(3)
<DD> We will recur with the value of n changed; specifically, we will
recur with n-m and m.
<P>
</DL>
<P>
These steps translate into the following function definition:
<BLOCKQUOTE>
<PRE> (defun remainder (n m)
(cond ((&lt; n m) n)
(t (remainder (- n m) m))))
</PRE>
</BLOCKQUOTE>
The following notation gives an idea of the execution of ``remainder'':
<BLOCKQUOTE>
<PRE>(remainder 30 7)
= (remainder 23 7)
= (remainder 16 7)
= (remainder 9 7)
= (remainder 2 7)
= 2
</PRE>
</BLOCKQUOTE>
</H4></b></b></b></b></H4></b></H4></b></b></b></b></b></H4></b></H4></b><BR> <HR>
<A HREF="node46.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
<A HREF="node41.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
<A HREF="node44.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="node46.html"> Ensuring Proper Termination</A>
<B>Up:</B>
<A HREF="node41.html"> Programming Techniques</A>
<B> Previous:</B>
<A HREF="node44.html"> Recursion on Nested </A>
<BR> <HR> <P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>