I'm struggling with an exercise from Haskell Book (Chapter 16. Functor). Given the following, it expects me to define fmap
:
{-# LANGUAGE FlexibleInstances #-}
newtype Flip f a b =
Flip (f b a)
deriving (Eq, Show)
newtype K a b =
K a
instance Functor (Flip K a) where
fmap = undefined
In a previous exercise I've already done the following:
data K a b =
K a
instance Functor (K a) where
fmap f (K x) = K x
But for this Flip a b
, I can't even understand how to get started, e.g. how to continue fmap f Flip ...
.
I thought maybe before doing that I should also write a functor for the newtype K a b
, similar to what I did data K a b
:
instance Functor (K a) where
fmap f (K x) = K x
But I can't understand how to proceed to the Functor instance for Flip f a b
.
Any ideas, hints?
instance Functor (Flip K a) where fmap f (Flip (K x)) = Flip (K (f x))
. But right now I can't explain it to myself (don't even know whether it is correct). – Emre Sevinç