gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:54:29 25 4
gpt4 key购买 nike

类 Applicative 声明为:

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

我们可以代表 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

在应用上,怎么能 <*>表示为 fmap_i, i=0,1,2,... ?

谢谢。

另见 Is the implementation of `<*>` based on `fmap` special to Maybe applicative or can it be generalized to other applicatives?

最佳答案

你可以写:

(<*>) = fmap2 ($)

或者,如果你觉得它不那么晦涩:
f <*> a = fmap2 apply f a
where apply g x = g x

关于haskell - 在应用中,如何用 `<*>` 表示 `fmap_i, i=0,1,2,...` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57188385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com