Deep recursion

brian_d_foy on 2003-01-16T16:51:12

The most interesting exerise answer I have ever received from a Learning Perl student got stung by deep recursion.

The exercise asked the student to write a subroutine to add numbers. I like the answer:

sub total
    {
    if( ! @_ )
        {
        return 0;
        }
    shift(@_) + total(@_);
    }


This worked fine for the short lists in the problem, but the next problem asked the student to use the subroutine to add the numbers 1 to 1000. Around 100 levels of recursion the program blew up.