[Haskell-beginners] Functor instance
sasa bogicevic
brutallesale at gmail.com
Mon Jan 9 18:07:17 UTC 2017
I will look at bimap too, sounds usefull, thanks!
Sasa Bogicevic
{
phone: +381606006200
}
> On Jan 9, 2017, at 17:40, David McBride <toad3k at gmail.com> wrote:
>
> There is also a BiFunctor in base which allows you to fmap over the
> last two variables in your type.
>
> bimap :: (a -> b) -> (c -> d) -> p a c -> p b d
>
> And it conveniently has methods to map over only one at a time, if you want.
>
> first :: (a -> b) -> p a c -> p b c
> second :: (b -> c) -> p a b -> p a c
>
> This makes sense for types like Either a b, where sometimes you might
> want to fmap over one or both values.
>
> However, going beyond two would cause the number of options for
> mapping over such a type to blow up. At that point it is better to
> just use your own simple function to map over your own type.
>
>> On Mon, Jan 9, 2017 at 11:26 AM, sasa bogicevic <brutallesale at gmail.com> wrote:
>> Good explanation! Thanks I understand it now
>>
>> Sasa Bogicevic
>> {
>> phone: +381606006200
>> }
>>
>>>> On Jan 9, 2017, at 17:00, Francesco Ariis <fa-ml at ariis.it> wrote:
>>>>
>>>> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote:
>>>> Hi all,
>>>> Can someone explain to me why exactly when defining the Functor
>>>> instance for type that has polimorphic parameter constraint I
>>>> am not allowed to put that parameter in definition. So basically:
>>>>
>>>> data Four a b c d = Four a b c d
>>>>
>>>> instance Functor (Four a b c) where <-- why can't I specify also
>>>> param d here ??
>>>> fmap f (Four a b c d) = Four a b c (f d)
>>>
>>> Hello Sasa,
>>> think for a moment about functors you already know: Maybe a, [a],
>>> (Either e) a, etc. In every case there is an `a` and something preceding
>>> a (we can call it `f`).
>>> Now let's look at the class-definition of functor.
>>>
>>> class Functor f where -- etc. etc.
>>>
>>> Here you have it, `f`; so for the instance you should only place
>>> the `f`-part in there, like
>>>
>>> instance Functor Maybe where -- not Maybe a!
>>> instance Functor (Four a b c) where -- without the a too!
>>>
>>> It makes sense as `f` will stay the same and `a` will be mapped over
>>> (and change).
>>> Indeed you can very this with ghci (using :k Functor, :k Maybe etc.),
>>> but after a while it sinks in.
>>>
>>> Does this help?
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
More information about the Beginners
mailing list