<?xml version="1.0" encoding="windows-1252"?>
<node id="335251" title="Re: Re: Pure Perl tail call optimization" created="2004-03-09 14:48:23" updated="2005-08-12 10:25:28">
<type id="11">
note</type>
<author id="108447">
demerphq</author>
<data>
<field name="doctext">
&lt;!--
&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;
&lt;code&gt;&lt;/code&gt;
&lt;i&gt;&lt;/i&gt;
&lt;b&gt;&lt;/b&gt;
&amp;#91; &amp;#93; 
--&gt;
&lt;p&gt;
Your code is better written as 
&lt;/p&gt;
&lt;code&gt;
sub loop_factorial {
    my ($n, $total) = @_;
    $total = 1 unless $total;
    while ($n) {
      ($n,$total) = ($n - 1, $total * $n);
    }
    return $total
}
&lt;/code&gt;
&lt;p&gt;
Which is more efficient, and is IMO a better example of what a compiler that optimizes tail recursion would convert it to. 
&lt;/p&gt;
&lt;br /&gt;
&lt;div class="pmsig"&gt;
&lt;div class="pmsig-108447"&gt;
---
&lt;br /&gt; 
demerphq&lt;br /&gt;
&lt;br /&gt;
&lt;sub&gt;

&lt;ol&gt;
&lt;p&gt;&lt;em&gt;First they ignore you, then they laugh at you, then they fight you, then you win. 
&lt;/em&gt;&lt;br /&gt; -- Gandhi&lt;/p&gt;
&lt;/ol&gt;
&lt;/sub&gt;
&lt;!--
&lt;hr /&gt;
&lt;sub&gt;
&lt;p&gt;
&lt;strong&gt;&amp;bull; Update:&amp;nbsp;&amp;nbsp;&lt;/strong&gt;&lt;br /&gt;

&lt;/p&gt;
&lt;/sub&gt;
--&gt;
&lt;br /&gt;

&lt;/div&gt;
&lt;/div&gt;</field>
<field name="root_node">
161611</field>
<field name="parent_node">
334306</field>
</data>
</node>
