Recursion
Recursion doesn't have to be hard
I get a lot of questions about recursion. For a lot of us, it's not something we've ever been taught or had to learn. It can feel unfamiliar and difficult.
But in functional programming, recursion is used quite a lot. It's not that different from iteration, and sometimes can be much easier to write, once you learn to see what's going on. It may be a struggle at first, but trust me, it's worth learning. You'll have more confidence dealing with nested data structures and recursive data.
In this introduction, I'm going to show you:
- how to write recursive functions, step-by-step
- the basic concepts so you never get lost
- the 2 types of recursion (non-tail-recursive and tail-recursive)
- the 3 parts of all recursions
- how recursion is like a for loop
Along the way, we'll implement several familiar functions in terms of recursion. We'll learn:
- how to maintain a seq's laziness when writing recursive functions
- how to avoid blowing the heap by using tail recursion
- how to convert regular recursion into tail recursion
10 Lessons