Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Basically, monads are also applicative functors [1]. So, whenever you find yourself using <code>liftM</code>, <code>liftM2</code>, etc., you could chain the computation together using <code>&lt;*&gt;</code>. In some sense, you can think of applicative functors as analogous to functions. A pure function <code>f</code> can be lifted by doing <code>f &lt;$&gt; x &lt;*&gt; y &lt;*&gt; z</code>.</p> <p>Compared to monads, applicative functors cannot run its arguments selectively. The side effects of all the arguments will take place.</p> <pre><code>import Control.Applicative ifte condition trueClause falseClause = do c &lt;- condition if c then trueClause else falseClause x = ifte (return True) (putStrLn "True") (putStrLn "False") ifte' condition trueClause falseClause = if condition then trueClause else falseClause y = ifte' &lt;$&gt; (pure True) &lt;*&gt; (putStrLn "True") &lt;*&gt; (putStrLn "False") </code></pre> <p><code>x</code> only outputs <code>True</code>, whereas <code>y</code> outputs <code>True</code> and <code>False</code> sequentially.</p> <p>[1] <a href="http://www.haskell.org/haskellwiki/Typeclassopedia" rel="noreferrer">The Typeclassopedia</a>. Highly recommended.</p> <p>[2] <a href="http://www.soi.city.ac.uk/~ross/papers/Applicative.html" rel="noreferrer">http://www.soi.city.ac.uk/~ross/papers/Applicative.html</a>. Although this is an academic paper, it's not hard to follow.</p> <p>[3] <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors" rel="noreferrer">http://learnyouahaskell.com/functors-applicative-functors-and-monoids#applicative-functors</a>. Explains the deal very well.</p> <p>[4] <a href="http://book.realworldhaskell.org/read/using-parsec.html#id652399" rel="noreferrer">http://book.realworldhaskell.org/read/using-parsec.html#id652399</a>. Shows how the monadic <code>Parsec</code> library can also be used in an applicative way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload