Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Stack depth & Recursion

From: jeff:rebol at: 10-Jul-2001 8:53

Howdy, Cal:
> After seeing this discussion I wrote a little hack to > implement tail recursion elimination. I'm sure it could be > done better (Joel? Ladislav? anybody wanna fix this thing > up?) but works well enough that I'll probably use it the > next time I need deep recursion. (wow.. up to 452000 > already) anyway, here it is, enjoy!
Neato! Inspired by your approach to the problem, below is my stab at it. -jeff ;------------------------------------------------------- do-tail-func: func [ "Does a tail-recursive function" word [word!] "The function name" args [block!] "Function arguments" locals [block!] "Function locals" body [block!] "Function body" args1 [block!] "Initial args to function" /local collect-args f ][ collect-args: func args compose/deep [throw reduce [(args)]] set word :collect-args f: function compose [[throw] (args)] locals body forever [args1: catch compose [f (args1)]] ] halt ;-examples: do-tail-func 'f1 [x][][ if x > 100000 [return "Done."] prin [x #] f1 2 * x ] [1] do-tail-func 'recfun [x] [] [ print x recfun x + 1 recfun x ] [1]