in reply to Syntactic sugar for tail call optimizations

If you want fancier applications of this pattern please refer to the links I gave.

Here's an even simpler implementation. sub f4{ return $_[0] }

Of course, as soon as the recursive sub starts doing anything remotely requiring recursion your 'pattern' falls apart like bad knitting.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Syntactic sugar for tail call optimizations
by LanX (Saint) on May 27, 2011 at 15:29 UTC
    > Of course, as soon as the recursive sub starts doing anything remotely requiring recursion your 'pattern' falls apart like bad knitting.

    And of course you can easily provide an example proving your polemic assertion...

    Cheers Rolf

      And of course you can easily provide an example proving your polemic assertion...

      Of course:

      use enum qw[ CODE GRAPH START PATH SEEN ]; sub _pathsFrom { return $_[CODE]->( @{ $_[PATH] }, $_[START] ) unless exists $_[GRAPH]->{ $_[START] }; for ( @{ $_[GRAPH]->{ $_[START] } } ) { if( exists $_[SEEN]->{ $_[START] . "-$_" } ) { return $_[CODE]->( @{ $_[PATH] }, $_[START] ); } else { _pathsFrom( @_[ CODE, GRAPH ], $_, [ @{ $_[PATH] }, $_[START] ], { %{ $_[SEEN] }, $_[START] . "-$_", undef } ); } } } sub pathsFrom(&@) { _pathsFrom( @_, [], {} ) }

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Ah I see a trivial example already remotely requiring recursion

        without sample data for your spontaneous example I can't test, but the following should do.

        use enum qw[ CODE GRAPH START PATH SEEN ]; sub _pathsFrom { _PATHSFROM: { return $_[CODE]->( @{ $_[PATH] }, $_[START] ) unless exists $_[GRAPH]->{ $_[START] }; for ( @{ $_[GRAPH]->{ $_[START] } } ) { if ( exists $_[SEEN]->{ $_[START] . "-$_" } ) { return $_[CODE]->( @{ $_[PATH] }, $_[START] ); } else { @_=( @_[ CODE, GRAPH ], $_, [ @{ $_[PATH] }, $_[START] ], { %{ $_[SEEN] }, $_[START] . "-$_", undef } ); redo _PATHSFROM; } } } }

        If not, enlighten us!

        UPDATE(18:16 CEST): This thread is about implementating tail call recursions, thanks for trolling with a graph-search recursion which isn't tail code.

        Cheers Rolf