O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with laziness and recursion

So far, we have seen a simple recursion; recursion using worker pattern. Haskell adds laziness to the mix. We can use laziness to our advantage while working with recursion. 

In this recipe, we will again calculate the fibonacci number. However, this time, we will do it with infinite lists. By taking advantage of Haskell's laziness to evaluate an expression only when it is required, we can really create a linear time algorithm (O(n)) for the fibonacci number.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required