Pattern 13 | Mutual Recursion |
Intent
To use mutually recursive functions to express certain algorithms, such as walking tree-like data structures, recursive descent parsing, and state machine manipulations
Overview
In Pattern 12, Tail Recursion, we looked at using tail recursion to walk over sequences of data and the tricks that Clojure and Scala use to avoid consuming stack frames while doing so, since the JVM doesn’t directly support tail recursion.
For the majority of cases, the simple tail recursion we looked at in Pattern 12, Tail Recursion, where the only recursive calls are self-recursive, is all we need. However, some of the more complex problems require solutions where functions can call each other recursively.
For instance,
Get Functional Programming Patterns in Scala and Clojure now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.