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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---