Hands-On with Infinite Fibonacci Numbers

Thunks, Streams, Recursion, Co-Recursion! There’s been a lot to learn in this chapter! Before we move on, let’s take a look at one more example to help reinforce the concepts you’ve learned in this chapter. The Fibonacci series is a sequence of numbers that frequently makes an appearance in programming texts because it has the valuable property of having a simple recursive definition. In this section, you’ll build an infinite stream of Fibonacci numbers, and then look at several different ways that you can refactor the code to be more efficient.

Let’s start by defining the Fibonacci sequence. It’s a sequence of integers starting with [0,1]. You can calculate each number in the series by adding up the ...

Get Effective Haskell 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.