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 preceding implementation looks deceptively simple, and it works! This is one of the highlights of Haskell. It provides a very elegant language construct to write such compact and meaningful programs in.

The heart of fiblist is the use of the zipWith function. The zipWith has the following signature:

    zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

The zipWith function takes a function f and two lists. The zipWith function takes out an element from each list and applies the funcion f on these two elements. The zipWith function recursively continues this operation until either of the input lists are exhausted. In fact, zipWith is implemented in Prelude as follows:

 zipWith f [] _ = [] -- Either input is empty, the result is ...

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