On 2/3/06, Dave Whipp <[EMAIL PROTECTED]> wrote:
> sub factorial(Int $x) {
> temp state Int $result = 1;
> $result *= $x;
> factorial $x-1 if $x > 2;
> return $result if want;
> }
> say factorial 6;
That's precisely what "env" variables are for. The right way:
sub factorial(Int $x) {
env $result = 1;
my sub fact(Int $x) {
$+result *= $x;
fact($x - 1) if $x > 2;
return $result;
}
fact($x);
}
Of course, you can view env variables as implicit parameters. Given
that, this function might be able to reduce to:
sub factorial(Int $x) {
env $+result = 1; # expecting $+result
# param, default to 1
$+result *= $x;
fact($x - 1) if $x > 2;
return $result;
}
Luke