Re: tail call optimization

2016-11-18 Thread Luca Ferrari
On Fri, Nov 18, 2016 at 4:57 AM, Andrew Kirkpatrick  wrote:
> But IIRC goto &func is more about fooling caller() than TCO, so its
> not that really fast thing some users expect.

Yes, it's more a way to not change the call stack than to optimize it.

Luca


Re: tail call optimization

2016-11-17 Thread Andrew Kirkpatrick
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"' 1000

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  wrote:
> Hiroki,
>
>> On 17 Nov 2016, at 10:49, Hiroki Horiuchi  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


Re: tail call optimization

2016-11-17 Thread Elizabeth Mattijsen
Hiroki,

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

tail call optimization

2016-11-17 Thread Hiroki Horiuchi
Hello.

I think the tail call is optimized in the following Perl 5 code.
How can I do the same in Perl 6?
--
#!/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;
--

Thanks.
--
Hiroki Horiuchi from Japan