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

Map function

In the recipe, we have implemented map in two ways. Both implementations are described here as follows:

  • The map function is defined as map f (x:xs) = f x : map f xs. Here, the mapping function f is applied to the first element, and then we will recurse using the remaining list and then join the result. Suppose the input list is [1,2,3,4,5], and we apply f to it. Then, our map will work in the following way. (Note that the function f is categorically not defined concretely to simplify the explanation of map.) As you can see, the result list keeps expanding towards the right (for example, f 1 : <rest of the list expanded here>):
        map f [1,2,3,4,5] = f 1 : map f [2,3,4,5]                  = f 1 : (f 2 : map f [3,4,5]) = f 1 : (f 2 : (f 3 : map ...

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