Mutual recursion on complementary predicates
Mutual recursion means two (or more) functions call each other. The textbook example is even and odd on natural numbers: even(n) is true if n==0, else odd(n-1); odd(n) is false if n==0, else even(n-1). The structure alternates—each function delegates to its partner on a strictly smaller argument.
In parsing and validation, mutual recursion shows up between grammar rules (“expression” calls “term”, “term” calls “factor”, “factor” may call “expression” again inside parentheses). For data scientists the full grammar machinery is overkill, but the pattern appears whenever two complementary shape predicates bounce between each other.
You still need ordinary base cases on both sides; mutual recursion does not remove the obligation to terminate.
PEP 8 (readability): [1].
Sources
Tasks
Card Info
- Topic: Recursion & structural decomposition
- Difficulty: Intermediate
- Completed: 0 users