<div dir="ltr">Unfortunately you are running in to strange behavior due to a lack of parenthesis. First of all, let's see what your original expression actually is:<div><br></div><div><font face="monospace, monospace">fmap (\x -> x) Just 2 = (fmap (\x -> x) Just) 2</font></div><div><br></div><div>So you can see that you are actually <font face="monospace, monospace">fmap</font>ing over <font face="monospace, monospace">Just</font>, rather than <font face="monospace, monospace">Just 2</font>. What does this mean? Well, let's ask GHC:</div><div><br></div><div><font face="monospace, monospace">:t fmap _ Just</font></div><div><div><font face="monospace, monospace"> Found hole `_' with type: Maybe a -> b</font></div></div><div><br></div><div>That is, when you try and <font face="monospace, monospace">fmap</font> over the <i>function</i> <font face="monospace, monospace">Just</font>, we have to provide a function that expects a <font face="monospace, monospace">Maybe a</font>, rather than an <font face="monospace, monospace">a</font>. In your first case, your providing the identity function, which fits the required type as <font face="monospace, monospace">Maybe a -> Maybe a</font>. However, in your second example, you are trying to provide apply the <font face="monospace, monospace">(+ 2)</font> function to a <font face="monospace, monospace">Maybe a</font> value (because<font face="monospace, monospace"> x :: Maybe a</font>). You cannot in general add numbers to <font face="monospace, monospace">Maybe a</font>, hence the error message.</div><div><br></div><div>Your final expression works because <font face="monospace, monospace">$</font> is effectively providing parenthesis:</div><div><br></div><div><font face="monospace, monospace">fmap (\x -> x + 2) $ Just 2 = fmap (\x -> x + 2) (Just 2)</font></div><div><br></div><div>The precedence of application in Haskell can be confusing at the start - I suggest liberally using parenthesis, and then using HLint to remove the ones that you don't need. Eventually, you'll build up the necessary intuition.</div><div><br></div><div>Hope this helps,</div><div>- Ollie </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 27, 2015 at 11:02 AM, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi, <div><br></div><div>Please can someone explain why these two expression behave differently - </div><div><br></div><div><font face="monospace, monospace">----</font></div><div><font face="monospace, monospace">fmap (\x -> x) Just 2<br></font></div><div><font face="monospace, monospace">Just 2</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">-----</font></div><div><font face="monospace, monospace">fmap (\x -> x+2) Just 2</font><br></div><div><br></div><div><div><i>No instance for (Num (Maybe a0)) arising from a use of `it'</i></div><div><i>In a stmt of an interactive GHCi command: print it</i></div></div><div><i><br></i></div><div>----</div><div>The first expression evaluates fine whereas the second one fails. However if I do - </div><div>----</div><div><br></div><div><span style="font-family:monospace,monospace">fmap (\x -> x+2) $ Just 2</span><br></div><div><span style="font-family:monospace,monospace">Just 4</span></div><div><span style="font-family:monospace,monospace">----</span></div><div><span style="font-family:monospace,monospace"><br></span></div><div><font face="arial, helvetica, sans-serif">Then the second expression also returns the Maybe value. Why is $ needed in second expression but not in the first one ?</font></div><div><span style="font-family:monospace,monospace"><br></span></div><div><span style="font-family:monospace,monospace">Thanks,</span></div><div><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font size="2" face="georgia, serif">Shishir </font></font><br></font><br></div></div></div>
</div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>