[Haskell-beginners] Monads
Patrick Lynch
kmandpjlynch at verizon.net
Wed Mar 2 22:38:07 CET 2011
----- Original Message -----
From: "Brent Yorgey" <byorgey at seas.upenn.edu>
To: <beginners at haskell.org>
Sent: Wednesday, March 02, 2011 10:37 AM
Subject: Re: [Haskell-beginners] Monads
> On Wed, Mar 02, 2011 at 10:04:38AM -0500, Patrick Lynch wrote:
>> Daniel,
>> Thank you...I understand now...
>> Good day
>>
>> Ps
>> I think my problem that I'm facing is that I don't see how the
>> instance Functor Maybe is derived from the class Functor...
>>
>> for example,
>> the author Miran Lipovaca gives the following:
>> class Functor f where
>> fmap::(a->b)->f a->f b
>>
>> if Maybe replaces f:
>> fmap::(a->b)->Maybe a->Maybe b
>
> Right.
>
>>
>> I don't see how the instance Functor Maybe is derived [presumably:
>> 'f' is function (a->b) and not functor 'f' as shown in the class
>> description):
>
> Yes, here 'f' is a function (a -> b). It is confusing that often the
> letter f is used both to represent an arbitrary Functor or to
> represent an arbitrary function. But you can tell the difference by
> seeing whether the f occurs in a type or in some code.
>
>> instance Functor Maybe where
>> fmap f (Just x) = Just (f x)
>> fmap f Nothing = Nothing
>
> So you are confused about this code? Can you be more specific what
> you are confused about? Try thinking carefully about all the types
> involved. What is the type of f? (You already answered this: a -> b.)
> What is the type of (Just x)? The type of x? What type is required on
> the right hand side of the = ? And so on.
>>>
>>>
>>>-Hi Brent
>>>I know that Maybe type is: data Maybe a = Nothing | Just a
>>>...so, I assume that the type to the right of the '=' should be Maybe a..
>>>...presumably, it should handle both Nothing and Just a
>>>...so I can see:
>>> instance Functor Maybe where
>>> fmap Nothing = Nothing
>>> fmap Just x = Just x
>>>...presumably, x can be any type, eg Int, String, Char...
>>>...so I don't see where 'f' comes from and I don't understand why (Just
>>>x) is in parenthesis...
>>>Thanks for your help, I'm beginning to see what I don't know...
>>>Good evening,
>>>-Pat
>>>
>>>
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list