46 lines
1.7 KiB
HTML
46 lines
1.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> Another Example</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Another Example">
|
|
<meta name="keywords" value="lp">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<BR> <HR>
|
|
<A HREF="node34.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A>
|
|
<A HREF="node30.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A>
|
|
<A HREF="node32.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="node34.html"> Iteration Using Dotimes</A>
|
|
<B>Up:</B>
|
|
<A HREF="node30.html"> Recursive Definitions</A>
|
|
<B> Previous:</B>
|
|
<A HREF="node32.html"> Using Trace To </A>
|
|
<BR> <HR> <P>
|
|
<H2> Another Example</H2>
|
|
<P>
|
|
Here's another recursive function. This one counts the number of elements of a list that are themselves lists:
|
|
<P>
|
|
<BLOCKQUOTE>
|
|
<PRE>(defun num-sublists (lis)
|
|
(cond
|
|
((null lis) 0) ;; stop if no elements
|
|
((listp (first lis)) ;; first is a list
|
|
(1+ (num-sublists (rest lis)))) ;; add to number in rest
|
|
(t (num-sublists (rest lis))))) ;; else count in rest
|
|
</PRE>
|
|
</BLOCKQUOTE>
|
|
<P>
|
|
Try this function out on various examples (and don't forget to use trace to see what it is doing). Also, think about what would happen if num-sublists is called with an atom as its argument, and how you might alter the definition to handle this.
|
|
<P>
|
|
<BR> <HR>
|
|
<P>
|
|
<ADDRESS>
|
|
<I>© Colin Allen & Maneesh Dhagat <BR>
|
|
March 2007 </I>
|
|
</ADDRESS>
|
|
</BODY>
|