Hiroki,
> On 17 Nov 2016, at 10:49, Hiroki Horiuchi <[email protected]> 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