Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: fmap Maybe (Alexey Shmalko)
2. Re: fmap Maybe (Oliver Charles)
----------------------------------------------------------------------
Message: 1
Date: Mon, 27 Apr 2015 10:12:44 +0000
From: Alexey Shmalko <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] fmap Maybe
Message-ID:
<CAFC2PC52fCfbi=h63xG=qul6jdjznt2hayc_546acfwdtxm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi, Shishir,
It's because Haskell uses juxtaposition for argument passing. So the first
case
fmap (\x -> x) Just 2
is equal to
(fmap (\x -> x) Just) 2
While
fmap (\x -> x+2) $ Just 2
is
fmap (\x -> x + 2) (Just 2)
I believe you want the latter.
Basically, the first example works because ((->) r) is an instance of
Functor.
instance Functor ((->) r) where
fmap = (.)
So basically first example is:
((\x -> x) . Just) 2
Now you should see why it behaves this way.
Have a nice day!
Alexey Shmalko
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150427/7e78229b/attachment-0001.html>
------------------------------
Message: 2
Date: Mon, 27 Apr 2015 11:13:16 +0100
From: Oliver Charles <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] fmap Maybe
Message-ID:
<cagrp5rkx_gknxrvqdxsrdvuovcnqhysx_v5hqu5cns+z2v6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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:
fmap (\x -> x) Just 2 = (fmap (\x -> x) Just) 2
So you can see that you are actually fmaping over Just, rather than Just 2.
What does this mean? Well, let's ask GHC:
:t fmap _ Just
Found hole `_' with type: Maybe a -> b
That is, when you try and fmap over the *function* Just, we have to provide
a function that expects a Maybe a, rather than an a. In your first case,
your providing the identity function, which fits the required type as Maybe
a -> Maybe a. However, in your second example, you are trying to provide
apply the (+ 2) function to a Maybe a value (because x :: Maybe a). You
cannot in general add numbers to Maybe a, hence the error message.
Your final expression works because $ is effectively providing parenthesis:
fmap (\x -> x + 2) $ Just 2 = fmap (\x -> x + 2) (Just 2)
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.
Hope this helps,
- Ollie
On Mon, Apr 27, 2015 at 11:02 AM, Shishir Srivastava <
[email protected]> wrote:
> Hi,
>
> Please can someone explain why these two expression behave differently -
>
> ----
> fmap (\x -> x) Just 2
> Just 2
>
> -----
> fmap (\x -> x+2) Just 2
>
> *No instance for (Num (Maybe a0)) arising from a use of `it'*
> *In a stmt of an interactive GHCi command: print it*
>
> ----
> The first expression evaluates fine whereas the second one fails. However
> if I do -
> ----
>
> fmap (\x -> x+2) $ Just 2
> Just 4
> ----
>
> Then the second expression also returns the Maybe value. Why is $ needed
> in second expression but not in the first one ?
>
> Thanks,
> Shishir
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150427/b9eee910/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 82, Issue 40
*****************************************