use strict;
eat('Ahh ... recursion!');
sub eat {
my $food = shift;
if ($food) {
print "$food\n";
eat (substr($food,0,length($food)-1));
}
else {
# base case
return;
}
}
####
use strict;
my @stack = ('Ahh ... recursion!');
eat() while @stack;
sub eat {
my $food = pop @stack;
if ($food) {
print "$food\n";
push @stack, substr($food,0,length($food)-1);
}
}
##
##
use strict;
my $numb = shift || 20;
print fib($numb), "\n";
sub fib {
my $n = shift;
return 1 if $n < 2;
return fib($n - 2) + fib($n - 1);
}
##
##
if (length $food) ...
# instead of
if ($food) ...