写这样的东西很好用:
data Either a b = Left a | Right b
instance Functor (Either a) where
fmap _ (Left x) = Left x
fmap f (Right x) = Right (f x)
现在假设我想反转这个,Left 将 f 应用于值:
instance Functor (Either a) where
fmap _ (Right x) = Right x
fmap f (Left x) = Left (f x)
这不编译,我想我需要有类似的东西Functor (Either _ b)
,我该怎么做?