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 Data.Functor module defines the type class Functor. The Functor class is available from Prelude, but defined in Data.Functor. The Functor type class is defined as follows:

    class Functor (f :: * -> *) where      fmap :: (a -> b) -> f a -> f b      (<$) :: a -> f b -> f a      {-# MINIMAL fmap #-}

Minimal definition of Functor requires fmap to be defined. 

The function fmap takes a function a -> b and takes a data type f parameterised by aIt then applies the function to the content (of the type a) to produce b, thus producing f b. 

For example, we can take Maybe a, which is defined as Just a | Nothing. When we apply a function a -> b to Maybe a through fmap, we would like to apply only for the data in the constructor Just a, producing  ...

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