

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Recursion
incomplete
2: Recursion Review
incomplete
3: Zipmap
incomplete
4: Recursion Quiz
incomplete
5: Nested Sum
incomplete
6: Recursion Review
incomplete
7: Recursion on a Tree
incomplete
8: Dangers of Recursion
incomplete
9: Recursion Practice
incomplete
10: Recursion Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Recursion is so dang useful with tree-like structures because we don't always know how deeply they're nested. Stop and think about how you would write nested loops to traverse a tree of arbitrary depth... it's not easy, is it?
for item in tree:
for nested_item in item:
for nested_nested_item in nested_item:
for nested_nested_nested_item in nested_nested_item:
# ... WHEN DOES IT END???
I most often use recursion on tree-like problems (file systems, nested dictionaries, etc.). If I'm just iterating over a one-dimensional list, then a loop (gasp...) is typically simpler, even if it's not as "pure" in the academic sense.
Remember: the rules of functional programming are just philosophies to help you write better code, but it's not always the right tool for the job. The same goes for any programming paradigm.