Re: On recursion
- Posted by Al Getz <Xaxo at ?ol?com> Jul 21, 2007
- 556 views
DB James wrote: > > Hi, > > To be sure, recursion is sometimes the way to go. I recently found myself > using the pattern below several times in writing string functions (probably > discovered a hundred times by Euphorians): > > }}} <eucode> > --====================================================================== > global function StringFunc(sequence s) > --basic pattern of several functions > --handles 1 or many strings in s > if not length(s) then > return s > elsif atom(s[1]) then > --process single string > else > for i=1 to length(s) do > s[i]=StringFunc(s[i]) > end for > end if > return s > end function > --====================================================================== > </eucode> {{{ > > Some recursive functions are obscure, but this pattern is clear and > obviously useful when processing one string or many. > > --Quark Hi DB and CChris, Recursion can be very useful when the underlying system can take the stack build up, and im pretty sure Euphoria can. Im not sure how this affects speed however, once the RAM gets filled (any system). In any case, I have found recursive use of functions to be quite handy in the past too. For example, i had to call a function that performed a rather intensive algorithm, but inside it had to decide whether the results of that algo were accurate enough to return a value or if it should switch to a smaller step size (like 1/10 the original) and perform the same algo only on each of the new (smaller) 10 steps (the algo gets more and more accurate as the step size is reduced, but small step size is not required to get accurate results over all the time frame). This doesnt sound too hard to do, but what makes it interesting is that once *those* 10 steps are started, it may become necessary to break one of those steps into 10 parts too, and then that could happen also with those 10 parts too, etc. It requires much less code to write this kind of thing as a recursive function so that the function can perform the accuracy test and then call itself 10 times if need be, and within those 10 new smaller parts perform the same test and possibly call itself again 10 more times for each of those larger 10 parts (or just some of them). It worked out pretty nicely. Take care, Al E boa sorte com sua programacao Euphoria! My bumper sticker: "I brake for LED's" From "Black Knight": "I can live with losing the good fight, but i can not live without fighting it". "Well on second thought, maybe not."