Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 1
Назад: Exercises
Дальше: Recurse Instead of Loop

Chapter 10
Recursively Recurse with Recursion

Recursion is a key concept in computer science that will unlock the more advanced algorithms we’re going to encounter in this book. When used correctly, recursion can be used to solve certain types of tricky problems in surprisingly simple ways. Sometimes, it even seems like magic.

But before we dive in, a pop quiz!

What happens when the blah() function defined here is called?

 def​ ​blah​():
  blah()

As you probably guessed, it will call itself infinitely, since blah() calls itself, which in turn calls itself, and so on.

Recursion is the term for a function calling itself. Indeed, infinite recursion, as in the above example, is utterly useless. When harnessed correctly, though, recursion can be a powerful tool.

Назад: Exercises
Дальше: Recurse Instead of Loop