[Haskell-beginners] Monads

Patrick Lynch kmandpjlynch at verizon.net
Wed Mar 2 16:04:38 CET 2011


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

  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):
     instance Functor Maybe where
       fmap f (Just x) = Just (f x)
       fmap f Nothing = Nothing

 the author's examples [presumably: 'f'' = (++ " HEY GUYS IM INSIDE THE JUST 
")  and 'x' = " Something serious ." and it associates from the right...]:
   ghci > fmap (++ " HEY GUYS IM INSIDE THE JUST ") ( Just " Something 
serious .")
            Just " Something serious . HEY GUYS IM INSIDE THE JUST "
   ghci > fmap (++ " HEY GUYS IM INSIDE THE JUST ") Nothing
            Nothing

I can continue on my Monad journey, just using the instance defintion for 
Functor Maybe - but it bothers me that I can't see its derivation...
I'd appreciate any advice.

Thank you

----- Original Message ----- 
From: "Daniel Fischer" <daniel.is.fischer at googlemail.com>
To: <beginners at haskell.org>
Cc: "Patrick Lynch" <kmandpjlynch at verizon.net>
Sent: Wednesday, March 02, 2011 9:22 AM
Subject: Re: [Haskell-beginners] Monads


> On Wednesday 02 March 2011 15:03:00, Patrick Lynch wrote:
>> In Learn You a Haskell for Great Good!, the author Miran Lipovaca
>> indicates that to understand Monads you need to know Functors... So, I
>> noticed the following:
>>
>> class Functor f where
>>   fmap::(a->b)->f a->f b
>>
>> instance Functor [] where
>>   fmap = map
>>
>> map::(a->b)->[a]->[b]
>>
>> if [] is substituted for f in the class definition of Functor the
>> following is obtained class Functor [] where
>>   fmap::(a->b)->[]a->[]b
>>
>> my questions are:
>>   1. is this substitution ok?
>>   2. is []a = [a]?
>>   3. is []b = [b]?
>>
>> if 2. and 3. are valid then the following is obtained:
>>   fmap::(a->b)->[a]->[b]
>> which is the same as map type and therefore: fmap = map. QED.
>>
>> Can you please answer questions 2 and 3 above?
>>
>> Thank you
>
> Yes, that's correct. The list constructor is special ([] isn't valid
> Haskell for a type constructor, so it's wired in), it can be used prefix
> ([] a) or `roundfix' ([a]). Ordinarily, you can use type constructors
> prefix or infix (if they take two type arguments), e.g. a -> b = (->) a b;
> Either a b = a `Either` b. 




More information about the Beginners mailing list