in reply to Tail recursion elimination

It's not an error, it's just a warning and you'd do better to just turn it off with no warnings 'recursion' than to do this ugly thing you just posted. I'm sure there's a valid reason to use goto &{...} to get around recursion in perl but I haven't seen it yet.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: Tail recursion elimination
by jdporter (Paladin) on Apr 28, 2006 at 20:03 UTC

    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.

      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")'
Re^2: Tail recursion elimination
by billh (Pilgrim) on Apr 28, 2006 at 20:25 UTC
    Ugly! :-)
    Ok, I admit I wasn't aware that the warning could be disabled, but in any case for truly deep recursion, such as when continuation passiing, you might want to do this rather than actually running out of memory/stack.
    Bill H
    perl -e 'print sub { "Hello @{[shift]}!\n" }->("World")'
Re^2: Tail recursion elimination
by blazar (Canon) on May 02, 2006 at 11:46 UTC

    Well maybe it's not that wise to advertise my worst node ever, since it has already been heavily donwvoted, but in it I had a program meant to run indefinitely and to alternate two "phases": a sleeping one and active one. Their respective behaviour was implemented with a pair of subs switching one another upon a certain condition through a magical goto. There may be more serious situations in which a similar scheme could be desirable, I guess.