在应用中,如何用fmap_i,i = 0,1,2,...来表示*? [英] In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,...`?
本文介绍了在应用中,如何用fmap_i,i = 0,1,2,...来表示*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Class Applicative声明为:
Class Applicative is declared as:
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
我们可以用pure
和(<*>)
表示fmapi, i=0,1,2,...
:
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,...
表示<*>
?
In applicative, how can <*>
be represented in terms of fmap_i, i=0,1,2,...
?
谢谢.
另请参见是基于fmap的`&*;>的实现可能是可应用的,还是可以推广到其他应用?
推荐答案
您可以编写:
(<*>) = fmap2 ($)
或者,如果您发现它不太晦涩,则:
or, if you find it less obscure:
f <*> a = fmap2 apply f a
where apply g x = g x
这篇关于在应用中,如何用fmap_i,i = 0,1,2,...来表示*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文