在应用中,如何用“fmap_i, i=0,1,2,...”来表示“<*>”?

2024-03-17

Applicative 类声明为:

class Functor f   =>  Applicative f   where
pure  ::  a   ->  f   a
(<*>) ::  f   (a  ->  b)  ->  f   a   ->  f   b

我们可以代表fmapi, i=0,1,2,...按照pure and (<*>):

fmap0 ::  a   ->  f   a
fmap0 =   pure
fmap1 ::  (a  ->  b)  ->  f   a   ->  f   b
fmap1 g   x   =   pure    g   <*> x
fmap2 ::  (a  ->  b   ->  c)  ->  f   a   ->  f   b   ->  f   c
fmap2 g   x   y   =   pure    g   <*> x   <*> y
fmap3 ::  (a  ->  b   ->  c   ->  d)  ->  f   a   ->  f   b   ->  f   c   ->  f   d
fmap3 g   x   y   z   =   pure    g   <*> x   <*> y   <*> z

在应用上,如何能<*>可以表示为fmap_i, i=0,1,2,...?

Thanks.

也可以看看基于“fmap”的“”实现对于 Maybe applicative 来说是特殊的还是可以推广到其他 applicative? https://stackoverflow.com/questions/57220345/is-the-implementation-of-based-on-fmap-special-to-maybe-applicative-or-c


你可以写:

(<*>) = fmap2 ($)

或者,如果您发现它不那么晦涩:

f <*> a = fmap2 apply f a
  where apply g x = g x
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在应用中,如何用“fmap_i, i=0,1,2,...”来表示“<*>”? 的相关文章

随机推荐