[Haskell-cafe] Functor instance for FunPB

Ruben Astudillo ruben.astud at gmail.com
Sat May 6 18:30:47 UTC 2017


On 06/05/17 14:50, Alex Wede wrote:
> Now I need to write a functor instance
>
> instance Functor (FunPB a) where
>
>     fmap f b = ?

It will help to see the specific types of the instance

    instance Functor (FunPB a) where

        fmap :: (c -> d) -> FunPB a c -> FunPB a d

ie we need to change only the second argument of FunPB a c. By you
definition, that would be the [b] in

    data FunPB a b = FunPB { runFunPB :: a -> (a,[b]) }

so you can remove the constructor to get the inner function, create a
new function that uses the inner one and fmap the second part of the
tuple

    instance Functor (FunPB a) where

        fmap f (FunPB lambda) =
            FunPB $ \a1 -> let (a2, bs) = lambda a1
                           in  (a2 , fmap f bs)

see that we have a `fmap f bs` in the second part, we use that we
already have a Functor instance for [b], making our job easier.


-- 
-- Ruben


More information about the Haskell-Cafe mailing list