[Haskell-beginners] Re: Thinking about monads
byorgey at seas.upenn.edu
Tue Apr 14 16:42:08 EDT 2009
On Mon, Apr 13, 2009 at 03:11:42PM -0700, Michael Mossey wrote:
> I know Maybe is both a functor and a monad, and I was thinking: what's the
> difference? They are both wrappers on types. Then I realized, the
> difference is: they have different class definitions.
In fact, every monad should be a functor, but not every functor is a
monad. Being a monad is a much stronger condition than being a functor.
> class Functor f where
> fmap :: (a->b) -> f a -> f b
> (Note how fussy this definition would be in C++. It would be a kind of
> template, but would probably look a lot more complex and would require
> lengthy declarations.)
> class Monad m where
> a >>= b :: m a -> (a -> m b) -> m b
Don't forget return :: a -> m a ! That's the other key method in the Monad
class. (There are also >> and 'fail' but those are unimportant---the
first is just a specialization of >>=, and fail is a hack).
More information about the Beginners