Recursion

In this lesson, we take a look at the concept of recursion and why it plays such an important role for programming in Haskell. We also examine the evaluation of expressions with recursive functions.

Introduction to recursion

Two fundamental concepts of imperative programming language are conditional statements (if,then,else) and loop statements (for, while). In pure functional programming, we have neither of these, as there are no statements, only expressions. However, the guarded equations of the previous chapter can be seen as the Haskell counterpart to if statements. Likewise, we can use recursion to solve problems that are usually tackled by loops in imperative languages.

A recursive function is a function which calls itself in at least one of its defining equations. As an example, let’s take another look at the compoundInterest function from the introduction:

Get hands-on with 1200+ tech skills courses.