Personal tools

Applicative functor

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Monad vs. applicative functor)
(Some advantages of Applicative)
Line 19: Line 19:
 
See Haskell-Cafe discussion.
 
See Haskell-Cafe discussion.
 
-->
 
-->
  +
  +
Some advantages of <hask>Applicative</hask>:
  +
* Code that uses only on the <hask>Applicative</hask> interface are more general than ones uses the <hask>Monad</hask> interface, because there are more applicative functors than monads.
  +
* Programming with <hask>Applicative</hask> has a more applicative/functional feel. Especially for newbies, it may encourage functional style even when programming with effects. Monad programming with <hask>do</hask> notation encourages a more sequential & imperative style.

Revision as of 08:00, 5 November 2007

An applicative functor has more structure than a functor but less than a monad. See the Haddock docs for <div class="inline-code">
Control.Applicative
</div>
.

It has turned out that many applications do not require monad functionality but only those of applicative functors. Monads allow you to run actions depending on the outcomes of earlier actions.

do text <- getLine
   if null text
     then putStrLn "You refuse to enter something?"
     else putStrLn ("You entered " ++ text)

This is obviously necessary is some cases, but in other cases it is disadvantageous.

Some advantages of
Applicative
:
  • Code that uses only on the
    Applicative
    interface are more general than ones uses the
    Monad
    interface, because there are more applicative functors than monads.
  • Programming with
    Applicative
    has a more applicative/functional feel. Especially for newbies, it may encourage functional style even when programming with effects. Monad programming with
    do
    notation encourages a more sequential & imperative style.