简体   繁体   English

Haskell 中的 *> 和 >> 有什么区别?

[英]What is the difference between *> and >> in Haskell?

From the docs:从文档:

(>>) : Sequentially compose two actions, discarding any value produced by the first (>>) :依次组合两个动作,丢弃第一个动作产生的任何值

(*>) : Sequence actions, discarding the value of the first argument. (*>) : 序列动作,丢弃第一个参数的值。

Both seem to me doing the same job.在我看来,两者都在做同样的工作。

They are equivalent, in practice.在实践中,它们是等效的。

Historically, Haskell did not have an Applicative typeclass (hence no *> ), but only a Monad typeclass (with >> ).从历史上看,Haskell 没有Applicative类型类(因此没有*> ),而只有Monad类型类(带有>> )。

At a certain point, Applicative was made a superclass of Monad .在某一时刻, Applicative成为Monad的超类。 At that point, it was introduced *> as a slightly more general variant of >> , which does not require one to work with a monad, but merely with an applicative functor.那时,它被引入*>作为>>的一个稍微更通用的变体,它不需要使用 monad,而只需使用 applicative functor。

(*>) :: Applicative f => f a -> f b -> f b
(>>) :: Monad f       => f a -> f b -> f b

The net result is that when working with applicatives, we can only use *> , while when working with monads (which are also applicatives) we can use either *> or >> interchangeably, since they are required to be equivalent in that case.最终结果是,在使用应用程序时,我们只能使用*> ,而在使用 monad(也是应用程序)时,我们可以互换使用*>>> ,因为在这种情况下它们必须是等价的。

Several other monad-related functions have been similarly generalized to applicatives:其他几个与 monad 相关的函数也被类似地推广到应用程序:

  • return is generalized by pure returnpure泛化
  • ap is generalized by <*> ap<*>概括
  • mapM is generalized by traverse mapM通过traverse泛化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM