Note that there are some explanatory texts on larger screens.

plurals
  1. POFind out how deep a call stack went in Clojure (or java)
    primarykey
    data
    text
    <p>Clojure is my first foray into the world of lisp. I decided to try a simple recursive function that raises a number to an integer power. Simple enough.</p> <pre><code>(defmulti pow #(compare %2 0)) (defmethod pow 0 [a n] 1) (defmethod pow 1 [a n] (* a (pow a (dec n)))) (defmethod pow -1 [a n] (/ 1 (pow a (* -1 n)))) </code></pre> <p>It works fine if you only pass integer powers. But I get stack overflows (predictably) when I use it to raise to a large enough power.</p> <p>The question is, how can I find out exactly how deep the function IS recursing before it dies? One way to get a ballpark on how deep I <em>can</em> recurse is by doing something like:</p> <pre><code>(defn max-depth [n] (try (max-depth (inc n)) (catch StackOverflowError err (do (printf "%s at %d\n" err n) n) ))) </code></pre> <p>(Clojure is my first foray in to lisp so I don't really know how to make the code look readable.) This code just recurses infinitely until it overflows the stack, then returns the number of recursions that took place before it blew up. This only gives me a ballpark figure of how deep I'm allowed to go... There are a lot of problems with this approach.</p> <p>Another thing I can do is catch the exception and try to unwind the stack myself... But I don't really see a way to get the information that I want from the exception. Looking at the javadoc for StackOverflowError at <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/StackOverflowError.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/lang/StackOverflowError.html</a> I see a few methods that look promising, but nothing panned out as far as I could see.</p> <p>I tried running (count (.getStackTrace error)) where error is the stackoverflow that I caught, but the result was just 1024 since "under some circumstances, stack frames may be ommited". So that didn't work.</p> <p>The only other thing I could think of would be to run pow over succesively larger exponents, but again that doesn't really tell me how deeply the call stack got, it only tells me how large of an exponent I can raise to (since the function calls other functions, the two answers are not the same).</p> <p>I don't know of any way to do it in java either, but if there is one, then that answer would also probably be helpful.</p> <p>Any ideas? Thanks, --Scott</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload