Thanks!

I just wanted to point out that there other reasons than speed to use tail calls.

But after spotting and correcting the typo you had in in f0() it becomes more evident

use Benchmark qw(cmpthese); use diagnostics; my $limit = 10_000; sub f0 { ++$_ < $limit ? &f0 : return $_ } sub f1 { ++$_ < $limit ? goto &f1 : return $_ } sub f2 {{ ++$_ < $limit ? redo : return $_ }} cmpthese -1, { f0 => sub { $_ = 0; f0() }, f1 => sub { $_ = 0; f1() }, f2 => sub { $_ = 0; f2() }, };

Deep recursion on subroutine "main::f0" at /home/lanx/B/PL/PM/tail_cal +l.pl line 6 (#1) (W recursion) This subroutine has called itself (directly or indir +ectly) 100 times more than it has returned. This probably indicates an infinite recursion, unless you're writing strange benchmark progra +ms, in which case it indicates something else. Rate f1 f0 f2 f1 76.1/s -- -31% -73% f0 111/s 46% -- -60% f2 280/s 267% 152% --

Tail calls reuse the current call frame and thus avoid stack overflows in deep recursions.

I was able to make my system heavily swapping and unresponsive with a limit of 1000000.

Cheers Rolf


In reply to Re^3: Syntactic sugar for tail call optimizations by LanX
in thread Syntactic sugar for tail call optimizations by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.