in reply to Re: Tail Recursion in Perl
in thread Tail Recursion in Perl

I'm going to play newbie and ask you for an example of how you think goto &traverse_rec; is tail-recursive.

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Tail Recursion in Perl
by Anonymous Monk on Dec 31, 2003 at 19:55 UTC
    > I'm going to play newbie and ask you for an example of how you think goto &traverse_rec; is tail-recursive.

    use warnings; sub recurse { my $i = shift; return if $i == 0; recurse($i-1); } sub tailless_recurse { my $i = shift; return if $i == 0; @_ = ($i-1); goto &tailless_recurse; } print "recursing\n"; recurse(200); print "tailless_recursing\n"; tailless_recurse(200);

    This produces the following output:

    recursing Deep recursion on subroutine "main::recurse" at /tmp/p line 7. tailless_recursing

    Note that the second one doesn't produce the deep recursion warning.

Re: Re: Re: Tail Recursion in Perl
by LunaticLeo (Scribe) on Dec 31, 2003 at 20:36 UTC
    Tail recursion refers to a function call being completely replaced on the stack by the next function it calls. Therefor when you recurse with a tail call your stack doesn't get deeper and deeper and deeper.

    This is the feature that goto &tail_call provides in perl's runtime environment.

    BTW, I totally aggree with the carpenter and bricklayer comment. :)

Re: Re: Re: Tail Recursion in Perl
by bsb (Priest) on Jan 02, 2004 at 00:50 UTC