Lesson 7. Rules for recursion and pattern matching
After reading lesson 7, you’ll be able to
- Understand the definition of a recursive function
- Learn the rules for writing recursive functions
- Walk through examples of recursive function definitions
- Use basic pattern matching to solve recursive problems
One of the first challenges of writing practical code in a functional language is that because you don’t have state changes, you also don’t have common looping functions that rely on changing state, such as for, while, and until loops. All iteration problems have to be solved through recursion. For many programmers, this is a terrifying thought, as recursion typically brings up memories of headache-inducing problem solving. Thankfully, you can use a few simple rules to make recursion much easier. Additionally, just as Haskell offers partial application to make closures easier to work with, Haskell provides a feature called pattern matching to make recursion much saner to reason about.