[Tutor] Re: Recursion....what are the best situations to use it?

Andrei project5 at redrival.net
Fri Apr 15 08:21:54 CEST 2005

 <jsoares <at> Safe-mail.net> writes:

> If this is too general a question, perhaps you can tell me when NOT to use
recursion(where it would be
> inappropriate or inefficient).

Easy way to kill a Python program:

>>> def a():
...     a()

Recursion is dangerous if its depth is unchecked. I've recently seen a recursive
quicksort implementation run wild for example, killing the program without any
error message (not in Python, but the principle is the same regardless the
programming language).

You can use recursion it for a shortest route algorithm, given a group of points
with certain connections between them (if there aren't too many points,
otherwise you'll hit a recursion depth limit or a stack overflow). Or for a menu
navigation system, where a showmenu() routine calls itself to display submenus.



More information about the Tutor mailing list