[Haskell-cafe] Network.CGI -- practical web programming example.
wren ng thornton
wren at freegeek.org
Thu Jul 2 17:59:44 EDT 2009
Brandon S. Allbery KF8NH wrote:
> Some Haskell
> programmers use fmap (because most Monads are also Functors), others use
> liftM. Both have the same effect: given a monadic computation "m a",
> "liftM f" turns "f" into a function that operates on the enclosed "a"
> instead of the entire "m a".
That is, given the theory behind it all, every monad is a functor (note
the lower case); from which it follows that liftM == fmap. For
historical reasons the Monad typeclass does not require a Functor
instance, however, and so it's not the case that every Monad is also a
Functor (note the upper case).
The function liftM can be defined generically given definitions for
return and (>>=), so some prefer to use liftM to avoid the extra Functor
dependency. The function fmap can be given specialized definitions due
to overloading, so others prefer to use it for efficiency reasons. The
(<$>) function is just a symbolic name for fmap.
You'll also see the Applicative typeclass for "applicative functors".
Applicative does require a Functor instance, which is good. (And
actually, every monad is an applicative functor; though the Monad class
doesn't require Applicative either.) The function liftA can be defined
generically given definitions for pure and (<*>), and liftA == fmap as
well. The only reason anyone should use liftA is for defining a Functor
instance when they're too lazy to give a specialized implementation.
More information about the Haskell-Cafe