This one causes headaches to me:

`#!/usr/bin/perl
use strict;
use warnings;
print "iterFac_for:\t", iterFac_for(6), "\n";
print "recFac:\t\t", recFac(6), "\n";
print "iterFac:\t", iterFac(6), "\n";
exit;
sub iterFac_for { # straight iterativ (perl-style)
my ($max, $val) = (shift, 1);
$val *= $_ for (1..$max);
return $val;
}
sub recFac { # linear recursive
my $n = shift;
return 1 if $n == 1;
$n *= recFac($n-1);
}
sub iterFac { # linear iterativ but recursive defined?!
my $max = shift;
my $val = shift || 1;
my $cnt = shift || 1;
return $val if $cnt > $max;
iterFac( $max, $val*$cnt, ++$cnt );
}
`

As you can see, all of those functions calculates factorial of a given number. The first is the way I would solve this kinda task in perl, the second one is recursive and I would say using recursion in this case is deprecated (even if it isn't so bad as calculating fibonacci-numbers recursive) and the last one is linear iterative but recursive defined. I found it in the SICP-book

http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.1 and choosed to code it in perl instead of scheme. What gives me a headache is what they say about the implementation in scheme allows tail-recursion for the last function, that means it doesn't create an overhead on the stack for each recursive call of this function. Is there something similar in perl, or is it useless cause we have different ways to build iterative functions (as my first example)?

Comment onTail-recursion in perl?!DownloadCode