haskell - 在应用中,`<*>`如何用`fmap_i, i=0,1,2,...`来表示呢?
问题描述
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,...
?
谢谢。
另请参见基于 `fmap` 的 `<*>` 的实现对于 Maybe applicative 是特殊的还是可以推广到其他 applicative?
解决方案
你可以写:
(<*>) = fmap2 ($)
或者,如果您发现它不那么晦涩:
f <*> a = fmap2 apply f a
where apply g x = g x
推荐阅读
- rethinkdb - 有什么方法可以使用 createQuery 应用组和分页吗?
- c - 将 C SW 模块作为源代码与库提供
- ios - 使用 MobileIron 在 iOS 12 中启动屏幕后 Cordova 应用程序崩溃
- python - 如何通过在单个单元格中删除 NaN 来调整数据框的大小?
- php - 通过 hasManyThrough Laravel Eloquent 获取多个表
- python - 如何使用 python API 将文件嵌入到数据流作业中
- python - Django 管理操作:仅使用一种方法为所有选择生成操作
- android - 启动时重新启动服务 8.1 问题
- android - 房间数据库中的外键
- c# - 如何使用 DisplayFormat 属性格式化表中的值?