fmap和bind之间的关系 [英] Relationship between fmap and bind

查看:73
本文介绍了fmap和bind之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查找 Control.Monad 文档,我很困惑 此段落:

After looking up the Control.Monad documentation, I'm confused about this passage:

以上法律暗示:

The above laws imply:

fmap f xs = xs >>= return . f

它们如何暗示?

推荐答案

Control.Applicative

由于这些定律,f的Functor实例将满足

fmap f x = pure f <*> x

ApplicativeMonad之间的关系说

pure = return

(<*>) = ap

ap

return f `ap` x1 `ap` ... `ap` xn

等同于

liftMn f x1 x2 ... xn

因此

fmap f x = pure f <*> x
         = return f `ap` x
         = liftM f x
         = do { v <- x; return (f v) }
         = x >>= return . f

这篇关于fmap和bind之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆