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

Tim*_*Tim 4 haskell functor applicative

类Applicative声明为:

class Functor f   =>  Applicative f   where
pure  ::  a   ->  f   a
(<*>) ::  f   (a  ->  b)  ->  f   a   ->  f   b
Run Code Online (Sandbox Code Playgroud)

我们可以表示fmapi, i=0,1,2,...在条款pure(<*>)

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
Run Code Online (Sandbox Code Playgroud)

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

谢谢。

另请参见基于fmap的<*>实现是否可能是可应用的,还是可以推广到其他应用?

K. *_*uhr 9

你可以写:

(<*>) = fmap2 ($)
Run Code Online (Sandbox Code Playgroud)

或者,如果您发现它不太模糊:

f <*> a = fmap2 apply f a
  where apply g x = g x
Run Code Online (Sandbox Code Playgroud)

  • 您正确地怀疑,但是那将是巨大的过大杀伤力。您只需要`pure`(您的`fmap0`)和`&lt;*&gt;`或`liftA2`(您将其称为“ fmap2`”)来定义一个Applicative。那么,为什么要在类中添加无数种不同的多余方法呢?实际上,这是“提升”两个以上参数的功能的能力,而“ Applicative”实际上是所有功能,而“ Functor”只能对单个参数的功能执行。(顺便说一句,您的`fmapk`在标准库中为`liftAk`,对于`k`的范围是2到任意数字(我认为是5左右)。) (3认同)