Search⌘ K
AI Features

Recursion

Explore how recursion serves as a fundamental technique in Haskell for writing pure functions without loops or statements. Understand the evaluation process of recursive functions, the role of base and recursive cases, and how Haskell ensures consistent results through referential transparency. This lesson equips you with the skills to write and reason about recursive functions effectively.

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 ...