- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Typeclassopedia提出以下练习:
Implement pure and (<*>) in terms of unit and (**), and vice versa.
这是Monoidal
和MyApplicative
:
class Functor f => Monoidal f where
u :: f () -- using `u` rather than `unit`
dotdot :: f a -> f b -> f (a,b) -- using instead of `(**)`
class Functor f => MyApplicative f where
p :: a -> f a -- using instead of `pure`
apply :: f (a -> b) -> f a -> f b -- using instead of `(<**>)`
首先,让我展示 Maybe
-类似数据类型:
data Option a = Some a
| None deriving Show
然后,我定义了instance MyApplicative Option
:
instance MyApplicative Option where
p = Some
apply None _ = None
apply _ None = None
apply (Some g) f = fmap g f
最后,我尝试实现 Monoidal Option
就p
而言和apply
的MyApplicative
:
instance Monoidal Option where
u = p ()
dotdot None _ = None
dotdot _ None = None
dotdot (Some x) (Some y) = Some id <*> Some (x, y)
这是对的吗?我的实现dotdot
与 apply
似乎没有
instance Monoidal Option where
u = p ()
dotdot None _ = None
dotdot _ None = None
dotdot (Some x) (Some y) = apply (Some id) (Some (x, y))
特别是,我很好奇如何正确实现 dotdot :: f a -> f b -> f (a, b)
与应用程序的(<*>)
- 就我而言,它是 apply
.
最佳答案
Applicative
是 Monoidal
的简洁替代演示文稿。两个类型类是等效的,您可以在两者之间进行转换,而无需考虑特定的数据类型,例如 Option
。 Applicative
的“简洁替代演示”基于以下两个等价
pure a = fmap (const a) unit
unit = pure ()
ff <*> fa = fmap (\(f,a) -> f a) $ ff ** fa
fa ** fb = pure (,) <*> fa <*> fb
获得 Applicative
的“简洁替代演示文稿”的技巧与 zipWith
的技巧相同- 将接口(interface)中的显式类型和构造函数替换为可以传递类型或构造函数以恢复原始接口(interface)的内容。
unit :: f ()
替换为pure
我们可以替换类型 ()
和构造函数() :: ()
进入恢复unit
.
pure :: a -> f a
pure () :: f ()
类似地(尽管不那么简单)替换类型 (a,b)
和构造函数(,) :: a -> b -> (a,b)
进入liftA2
恢复**
.
liftA2 :: (a -> b -> c) -> f a -> f b -> f c
liftA2 (,) :: f a -> f b -> f (a,b)
Applicative
然后得到很好的<*>
运算符(operator)通过提升功能应用($) :: (a -> b) -> a -> b
进入仿函数。
(<*>) :: f (a -> b) -> f a -> f b
(<*>) = liftA2 ($)
从 <*>
出发返回liftA2
很常见, liftA2
is included in Control.Applicative
。 <$>
是中缀 fmap
.
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
liftA2 f a b = f <$> a <*> b
关于haskell - 在应用方面实现 Monoidal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27262232/
也许这些陈述都不是绝对精确的,但是单子(monad)通常被定义为“内仿函数类别中的一个幺半群”; Haskell Alternative 被定义为“应用仿函数上的幺半群”,其中 applicative
Applicative 是一个 Monoidal Functor : mappend :: f -> f -> f $ :: (a -> b) -> a ->
假设我有一个函数列表 E => Either[Exception, Unit] 在事件 E 上调用并累积错误以返回 Either[List [异常(exception)],单位]。 type Even
在 Scala 函数式编程一书中,我看到了以下签名: def productMonoid[A,B](A: Monoid[A], B: Monoid[B]): Monoid[(A,B)] 描述是这样说的
从下面的例子中,我认为说 String 在串联操作下定义了一个幺半群是正确的,因为它是一个关联二元操作,而 String 恰好有一个标识元素,它是一个空字符串 "" 。 scala> ("" + "J
Typeclassopedia提出以下练习: Implement pure and () in terms of unit and (**), and vice versa. 这是Monoidal和M
从下面的例子中,我认为说 String 在串联操作下定义了一个幺半群是正确的,因为它是一个关联二元操作,而 String 恰好有一个标识元素,它是一个空字符串 "" 。 scala> ("" + "J
我之前定义了一个函数,它接受一个列表 Maybe s 并将其变成 Maybe一个列表,像这样: floop :: [Maybe a] -> Maybe [a] floop [] = Just [] f
HLearn 的自述文件指出 Monoid 类型类用于并行批量训练。我在几个文件中看到了 trainMonoid ,但我很难剖析这个巨大的代码库。有人可以用初学者友好的术语解释它是如何工作的吗?我猜这
假设我有如下的 Monoid 特征: trait Monoid[A] { def combine(a1: A, a2: A): A def identity: A } 现在如果我想为此写一个
在 haskell IO 类型中有 Monoid 的实例: instance Monoid a => Monoid (IO a) where mempty = pure empty 如果我有三
trait Monoid[A] { def op(a1: A, a2: A): A def zero: A } def mapMergeMonoid[K, V](V: Monoid[V]):
我正在研究这个 Functional Programming in Scala练习: // But what if our list has an element type that doesn't
我试图以更实际的方式理解 monads 和 monoids 之间的关系。如果这个问题没有意义,我提前道歉,我仍在挣扎。 假设例如,我有: trait Monoid[T] { def zero: T
我想写一个方法mergeKeys将值分组在 Iterable[(K, V)] 中由 key 。例如,我可以写: def mergeKeysList[K, V](iter: Iterable[(K,
我正在阅读一本书,内容如下: sealed trait Currency case object USD extends Currency ... other currency types case
我在具有 SceneGraph 类型字段“_scene”的记录上使用下面的代码。我使用 makeLenses 为它创建了镜头。 inputGame :: Input -> Game -> Game i
我有一个数据类型是 Monoid 的实例所以我可以得到很好的值(value)观组合: data R a = R String (Maybe (String → a)) instance Monoid
根据定义或幺半群,二元运算符必须是关联的,例如A op (B op C) == (A op B) op C . base mconcat definition在 haskell 是: mconcat
这个问题在这里已经有了答案: Why MonadPlus and not Monad + Monoid? (4 个回答) 6年前关闭。 我对 Monads 和 Monoids 都很陌生,最近还了解了
我是一名优秀的程序员,十分优秀!