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 > > > On Sat, Nov 29, 2008 at 6:42 AM, bOR_ <[EMAIL PROTECTED]> wrote: >> >> In this case, the depth of the recursion would be at maximum 21 >> (number of different types of amino acids), and the function itself >> not often called. Is stack size something to worry about at those >> depths? >> >> On Nov 29, 11:11 am, "Kevin Downey" <[EMAIL PROTECTED]> wrote: >>> the jvm does not do TCO, loop/recur allows for functional looking >>> recursion on the jvm with constant stack size. >>> >>> >>> >>> On Sat, Nov 29, 2008 at 1:25 AM, bOR_ <[EMAIL PROTECTED]> >>> wrote: >>> >>>> Hi all, >>> >>>> I wondered if there is a difference between using loop-recur or >>>> merely >>>> writing a recursive function. The main difference I found thus >>>> far was >>>> that the loop-recur can suffice with less arguments, but the >>>> recursive >>>> functions seem to be shorter, and perhaps more elegant? >>> >>>> (defn construct-atom >>>> "translates a number n into an set of letters of size n" >>>> [construct length] >>>> (if (< (count construct) length) >>>> (construct-atom (conj construct (char (+ (rand-int amino_acids) >>>> 65))) length) >>>> construct)) >>> >>>> (defn construct-atom-loop >>>> "translates a number n into an set of letters of size n" >>>> [n] >>>> (let [base_construct #{}] >>>> (loop [construct base_construct] >>>> (if (< (count construct) n) >>>> (recur (conj construct (char (+ (rand-int amino_acids) 65)))) >>>> construct)))) >>> >>> -- >>> And what is good, Phaedrus, >>> And what is not good— >>> Need we ask anyone to tell us these things? >>> >> > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---