[Haskell-cafe] Re: Seeking advice about monadic traversal functions

Heinrich Apfelmus apfelmus at quantentunnel.de
Tue Apr 6 09:31:46 EDT 2010

Darryn Reid wrote:
> Martijn van Steenbergen wrote:
>> A small remark: I prefer using applicative notation for this:
>>> go Next (Single x t1) = Single x <$> rewrite f t1
>>> go Next (Fork t1 t2 ) = Fork     <$> rewrite f t1 <*> rewrite f t2
> Thanks for your comment and advice. Could you explain a little further
> your thinking? Specifically, what advantage do you find in the
> applicative notation, and when would you advise using it and when would
> you advise not using it?

The applicative notation is more general since it also applies to
applicative functors


It's main advantage over the liftM family is that it can be used with
any number of arguments

  liftM  f m     = f <$> m
  liftM2 f m n   = f <$> m <*> n
  liftM3 f m n o = f <$> m <*> n <*> o

and that's why I prefer it as well. It's very similar to function
application, too, just think of  <*>  as a replacement for the empty
space that separates function arguments.

The only drawback is probably that you have to

  import Control.Applicative

In fact, it doesn't actually work for monads, I think you have to wrap
it in a newtype. :D The same effect can be achieved with `ap` , though:

  liftM3 f m n p = return f `ap` m `ap` n `ap` o

Heinrich Apfelmus


More information about the Haskell-Cafe mailing list