Perl5 goto &func definitely doesn't grow the stack:

perl -E '$n = 0; sub wah { return if $_[0] < 1; $n++; @_=($_[0]-1);
goto &wah; }; wah(shift); say "done $n"' 10000000

But IIRC goto &func is more about fooling caller() than TCO, so its
not that really fast thing some users expect.

On 18 November 2016 at 07:11, Elizabeth Mattijsen <l...@dijkmat.nl> wrote:
> Hiroki,
>
>> On 17 Nov 2016, at 10:49, Hiroki Horiuchi <x19...@gmail.com> wrote:
>> I think the tail call is optimized in the following Perl 5 code.
>
> Are you sure?  Have you benchmarked it?  I seem to recall that using goto 
> like that in Perl 5 only makes sure that any backtrace doesn’t get longer.  
> But that it comes at significant overhead.
>
>
>> How can I do the same in Perl 6?
>
> Perl 6 currently does not have goto.
>
>
>> #!/usr/bin/env perl
>>
>> v5;
>>
>> use strict;
>> use warnings;
>>
>> local $\ = "\n";
>>
>> sub reduce_sum($$)
>> {
>>       my ($sum, $range) = @_;
>>       return $$sum unless @$range;
>>       my $lhs = shift @$range;
>>       $$sum += $lhs;
>>       goto &reduce_sum;
>> }
>>
>> my @range = 0 .. 10;
>> my $sum = 0;
>>
>> print reduce_sum \$sum, \@range;
>
> If you’re looking at solving the particular problem of reducing an array 
> using an operator:
>
> use v6;
> my $sum = [+] @range;
> say $sum;
>
>
>
> Hope this helps,
>
> Liz

Reply via email to