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

56 lines
2.2 KiB
HTML
Raw Normal View History

2022-08-02 12:34:59 +02:00
<!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> Using Trace To Watch Recursion</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Using Trace To Watch Recursion">
<meta name="keywords" value="lp">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR>
<A HREF="node33.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="node31.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="node33.html"> Another Example</A>
<B>Up:</B>
<A HREF="node30.html"> Recursive Definitions</A>
<B> Previous:</B>
<A HREF="node31.html"> A Simple Example</A>
<BR> <HR> <P>
<H2> Using Trace To Watch Recursion</H2>
<P>
Lisp provides a nice way to watch the recursive process using trace. Enter the following:
<BLOCKQUOTE>
<PRE>&gt; (trace power)
POWER
&gt; (power 3 4)
1&gt; (POWER 3 4) ;; Your actual output
2&gt; (POWER 3 3) ;; may vary in format
3&gt; (POWER 3 2)
4&gt; (POWER 3 1)
5&gt; (POWER 3 0)
&lt;5 (POWER 1)
&lt;4 (POWER 3)
&lt;3 (POWER 9)
&lt;2 (POWER 27)
&lt;1 (POWER 81)
81
</PRE>
</BLOCKQUOTE>
In this output, each number at the beginning of the line represents the depth of the recursion, from the top-level to the deepest call. All calls to power are documented, along with the values returned. This is a useful debugging tool to see whether your recursive function is doing what you think it should be doing.
<P>
If you redefine power and want to keep on watching the trace, then the instruction to (trace power) may have to be repeated. If you want to turn trace off, then (untrace power) will turn off trace for that specific function; (untrace) with no arguments will turn off trace for all functions you may have traced.
<P>
<BR> <HR>
<P>
<ADDRESS>
<I>&#169; Colin Allen &amp; Maneesh Dhagat <BR>
March 2007 </I>
</ADDRESS>
</BODY>