Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 828
Hugo Avatar asked Mar 31 '21 09:03

Hugo


1 Answers

They are equivalent, in practice.

Historically, Haskell did not have an Applicative typeclass (hence no *>), but only a Monad typeclass (with >>).

At a certain point, Applicative was made a superclass of 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.

(*>) :: 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.

Several other monad-related functions have been similarly generalized to applicatives:

  • return is generalized by pure
  • ap is generalized by <*>
  • mapM is generalized by traverse
like image 144
chi Avatar answered Oct 07 '22 01:10

chi