[Haskell-cafe] On to applicative

Alexander Solla ajs at 2piix.com
Thu Aug 26 14:15:42 EDT 2010


On Aug 26, 2010, at 9:27 AM, michael rice wrote:
> Some functions just happen to map to other functions.
>
> <$> is flip fmap.  f <$> functor = fmap f functor  #### Brent  
> Yorgey's post noted.
>
> #### "map to"? Take as arguments?

"maps to" as in "outputs".

> pure f <*> functor = f <$> functor
>
> #### Prelude Control.Applicative> pure double <*> (Just 5)
> #### Just 10
>
> #### Not so, the f got applied or where did we get the 10? Not sure,  
> is this the #### "mistake" you point out in your second post?


double is getting applied in that expression, but it isn't because of  
pure.  The <*> operator is pulling double out of pure double (which  
equals Just double in this case), and applying it to Just 5.

My correction was to point out that pure's type is more general than I  
had said.   Instead of
pure :: (a -> b) -> f (a -> b), it is pure :: a -> f a -- which  
includes (a -> b) -> f (a -> b) as a special case.  In fact, pure and  
return are "essentially equivalent" in form.  So you could write out  
your verification case as

(pure double <*> pure 5) :: Just Int

to further decouple your code from the particular functor you're  
working with.  (You need the type annotation to run it in GHCi)

>
> #### Two ways of doing the same thing?
>
> #### 1)
> #### Prelude Control.Applicative> (double <$> (Just 7))
> #### Just 14
>
> #### 2)
> #### Prelude Control.Applicative> pure double <*> (Just 7)
> #### Just 14

Indeed. 
  


More information about the Haskell-Cafe mailing list