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 to do it...

  1. Open src/Main.hs, and import Prelude, hiding two functions map and filter. These are the very functions we will implement:
        import Prelude hiding (map, filter)
  1. Write the declaration of map. A map is a function that takes a function, applies it to each member of the list, and returns the transformed list:
        map :: (a -> b) -> [a] -> [b]
Note the brackets around (a -> b). It denotes that it is a function that takes an argument of type a and returns a value of type bWithout the brackets, Haskell will interpret the function declaration differently.

The map function takes two arguments and a return value, as described:

  • The first argument (a -> b) is a transformer function
  • The second argument [a] is a list of a
  • The return ...

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