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 Applicative type class is defined as follows: 

    class Functor f => Applicative (f :: * -> *) where      pure :: a -> f a      (<*>) :: f (a -> b) -> f a -> f b      (*>) :: f a -> f b -> f b      (<*) :: f a -> f b -> f a      {-# MINIMAL pure, (<*>) #-}

The minimal definition of an Applicative instance requires at least pure and <*> to be defined. The definition also implies that we can define an instance of an Applicative for f only if f is also an instance of a Functor.

The pure function takes a value and creates a data type. For example, in the context of List, Maybe, and Either, the pure function will fetch the following values:

    pure 10 :: [Int] = [10]  -- in the context of List    pure 10 :: Maybe Int = Just 10 -- in the context of Maybe pure ...

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