根据以下内容 fp-course :
class Functor f where (<$>) :: (a -> b) -> f a -> f b class Functor f => Extend f where (<<=) :: (f a -> b) -> f a -> f b
我定义 <$$> 如此:
<$$>
(<$$>) :: Comonad f => (a -> b) -> f a -> f b (<$$>) f fa = f <$> fa
但是,我有兴趣知道是否还有其他方法可以实现 <$$gt; 不使用 <$> . 有?如果是,请出示!
<$$gt;
<$>
你需要 extract 方法 Comonad ; Extend 如果没有 fmap .
extract
Comonad
Extend
fmap
(<$$>) :: Comonad f => (a -> b) -> f a -> f b f <$$> w = f . extract <<= w
这基本上就是 liftW 在中实现 Control.Comonad .
liftW
Control.Comonad
还要注意你需要 <<= (或) extend ; 提取液 和 duplicate 还不够。情况类似于 Bind 和 Monad ;您可以实现 FMAP 使用 >>= 和 pure 但不使用 >>= 单独使用而不使用 join 和 纯净的 .
<<=
extend
提取液
duplicate
Bind
Monad
FMAP
>>=
pure
join
纯净的