<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Dave,<br><br>I wrote the first one sometime last year and it seemed a suitably simple example for applicative-izing to cement the ideas in some of the code I've been going though in "Learn You a Haskell ..." Interesting stuff.<br><br>Onward and upward.<br><br>Thanks,<br><br>Michael<br><br>--- On <b>Sat, 9/4/10, David Menendez <i>&lt;dave@zednenem.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: David Menendez &lt;dave@zednenem.com&gt;<br>Subject: Re: [Haskell-cafe] On to applicative<br>To: "michael rice" &lt;nowgate@yahoo.com&gt;<br>Cc: haskell-cafe@haskell.org<br>Date: Saturday, September 4, 2010, 2:23 PM<br><br><div id="yiv136714662">On Sat, Sep 4, 2010 at 2:06 PM, michael rice <span dir="ltr">&lt;<a rel="nofollow" ymailto="mailto:nowgate@yahoo.com" target="_blank"
 href="/mc/compose?to=nowgate@yahoo.com">nowgate@yahoo.com</a>&gt;</span> wrote:<br><div class="yiv136714662gmail_quote"><blockquote class="yiv136714662gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="font: inherit;" valign="top">The two myAction functions below seem to be equivalent and, for this small case, show an interesting economy of code, but being far from a Haskell expert, I have to ask, is the first function as small (code wise) as it could be?<br>
<br>Michael<br><br><br>import Control.Applicative<br><br>data Color<br>&nbsp;&nbsp;&nbsp; = Red<br>&nbsp;&nbsp;&nbsp; | Blue<br>&nbsp;&nbsp;&nbsp; | Green<br>&nbsp;&nbsp;&nbsp; | Yellow<br>&nbsp;&nbsp;&nbsp; | Orange<br>&nbsp;&nbsp;&nbsp; | Brown<br>&nbsp;&nbsp;&nbsp; | Black<br>&nbsp;&nbsp;&nbsp; | White<br>&nbsp;&nbsp;&nbsp; deriving (Show, Read, Eq, Enum, Ord, Bounded)<br>
<br>-- myAction :: IO Color<br>-- myAction = getLine<br>--&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;&gt;= \str -&gt; return (read str :: Color)<br></td></tr></tbody></table></blockquote><div><br></div><div>First, you don't need the type annotation here. Haskell will infer it from the annotation on myAction. (You also don't need the type on myAction, but that's not important now.)</div>
<div><br></div><div>myAction = getLine &gt;&gt;= \str -&gt; return (read str)</div><div><br></div><div>Second, you have the pattern \x -&gt; f (g x), so you can replace the lambda with function composition.</div><div><br>
</div><div>myAction = getLine &gt;&gt;= return . read</div><div><br></div><div>Third, there is a standard function liftM, defined in Control.Monad, where&nbsp;liftM f m = m &gt;&gt;= return . f, so</div><div><br></div><div>myAction = liftM read getLine</div>
<div><br></div><div>Finally, we expect an instance of Monad to also be an instance of Functor, with fmap = liftM, and we also have f &lt;$&gt; m = fmap f m, so</div><div></div><div>&nbsp;</div><blockquote class="yiv136714662gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="font: inherit;" valign="top">myAction :: IO Color<br>myAction = read &lt;$&gt; getLine</td></tr></tbody></table></blockquote></div><br>-- <br>Dave Menendez &lt;<a rel="nofollow" ymailto="mailto:dave@zednenem.com" target="_blank" href="/mc/compose?to=dave@zednenem.com">dave@zednenem.com</a>&gt;<br>
&lt;<a rel="nofollow" target="_blank" href="http://www.eyrie.org/%7Ezednenem/">http://www.eyrie.org/~zednenem/</a>&gt;<br>
</div></blockquote></td></tr></table><br>