9

There are a lot of functions that accomplish the same thing, but using Applicative vs Monad definitions.

Some examples are:

  • (<*>) vs ap
  • pure vs return
  • (*>) vs (>>)
  • traverse vs mapM
  • sequenceA vs sequence
  • liftA vs liftM
  • liftA2 vs liftM2
  • etc.

And for Alternative vs MonadPlus:

  • empty vs mzero
  • (<|>) vs mplus

In the scenario that prompted this question, I wanted to traverse/mapM where the Applicative/Monad is [].

In general, when it is known that the result will be the same, is it better style to use the Applicative ones or the Monad ones? E.g., is one more readable than another, or maybe faster than the other?

Edit (in response being suggested as a duplicate): Personally, I find these to be equally readable. I am in no way asking about maintainability, as these can be used interchangeably, and preferring one over another would have no impact on the code structure. I'm really wondering if one is more idiomatic, or more efficient, than the other.

zbw
  • 201

1 Answers1

11

For both efficiency and generality, you should typically prefer the most general operators. So you should use Applicative operators in preference to Monad, and Alternative to MonadPlus.

The efficiency difference is often not significant, so if you already have a Monad/MonadPlus constraint, you should feel free to use any of the relevant operators to produce the nicest looking code.

The ApplicativeDo extension can allow you to get the benefit of do-notation without a Monad requirement. I recommend the Haxl paper for an non-trivial example where there is a quite significant performance difference between Applicative and Monad (and was a motivator for implementing ApplicativeDo notation).