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

How it works...

The heart of the definition is as follows:

    fib n = fib (n-1) + fib (n-2)

Simple recursion involves calling the same function we are defining. In the definition of fib n, we will call fib (n-1) and fib (n-2) and add their results. 

The evaluation of the fibonacci number by this recursive definition is shown in the following diagram. The diagram shows the evaluation of fib 5. Note how at each stage, the fib function gradually reduces the argument and recursively calculates the value of the 5th fibonacci number:

The preceding function is a simple recursive function. One can also implement mutually recursive functions. For example, ...

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