On Sun, Nov 30, 2008 at 9:29 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > On Nov 29, 7:52 am, Rich Hickey <[EMAIL PROTECTED]> wrote: >> On Nov 29, 2008, at 6:49 AM, Daniel Renfer wrote: >> >> > Even if you don't think you'll run into the possibility of blowing >> > your stack, it's still a good idea to use recur when doing tail call >> > recursion. The compiler will help you out by making sure it really is >> > a tail call. >> >> > Remember, recur isn't just for loop. It works with functions too. >> >> In case that isn't clear, it means that anywhere you would do a self >> call in a tail position you can replace it with recur to ensure no >> stack growth, e.g. the first example could have been written: >> >> (defn construct-atom >> "translates a number n into an set of letters of size n" >> [construct length] >> (if (< (count construct) length) >> (recur (conj construct (char (+ (rand-int amino_acids) 65))) length) >> construct)) >> >> recur will goto the nearest enclosing loop or fn. >> >> Rich >> >> > > What about the case where recursing from a catch clause? > > e.g. > > (defn prompt-for-int [prompt junk-allowed default] > (let [input (prompt-read prompt)] > (try (Integer/valueOf input) > (catch NumberFormatException _ > (if junk-allowed > default > (prompt-for-int prompt junk-allowed default)))))) > > > If I use recur I get the following exception: > > java.lang.UnsupportedOperationException: Cannot recur from catch/ > finally > > I'm sure there is a more idiomatic way to write this. Maybe that's > the question I really need to ask? How can I rewrite the above?
I don't know the technical reason for disallowing recur from within a catch/finally. A more restricted case came up recently in this thread: http://groups.google.com/group/clojure/browse_thread/thread/2c9f018886e71ee3/05920a3ffd7250d2?lnk=gst#05920a3ffd7250d2 But that won't help with your case. I suppose you could use a helper function that returns nil if there was junk and then recur if you get nil back from the helper? -- Michael Wood <[EMAIL PROTECTED]> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---