in reply to Re: Tail recursion elimination
in thread Tail recursion elimination

There's a very good reason to eliminate tail recursion (in any language): to save memory if the recursion runs deep. Unfortunately, it only works when you have tail recursion.

The other "unfortunately" is that, at least the last time I checked, goto& was slower than recursing.

We're building the house of the future together.

Replies are listed 'Best First'.
Re^3: Tail recursion elimination
by diotalevi (Canon) on Apr 28, 2006 at 20:10 UTC

    You appear to have misunderstood. I didn't say that eliminating recursion was bad, I said that using goto &{...} nearly always is. There are probably better ways to do this in perl but one of the "best" ways I know of is to just go back to the top of the routine after setting @_ with the new args. For short routines and such.

    sub foo { { ... # Recurse but without actually calling foo() again. @_ = ...; redo; } }

    I also expect that there are better ways to save on memory than eliminating stack frames.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Fine if the routine is singly recursive, but if you have mutually recursive subroutines, then that won't work.
      Bill H
      perl -e 'print sub { "Hello @{[shift]}!\n" }->("World")'