[Haskell-beginners] WWAAA... I hate monads
jason.dusek at gmail.com
Wed Apr 22 08:31:33 EDT 2009
2009/04/22 Daniel Carrera <daniel.carrera at theingots.org>:
> 1) Am I right in thinking that any type with two elements
> ("m a") is always a Monad? (IO String, Maybe Int, etc).
Well, no. Many such types are monads but not all of them.
Monads have the monadic operations `return` and `>>=`.
> 2) When I say "m a >>= f" the >>= just pulls the "a" out of "m
> a" and passes it to "f". Right?
Yes. The `>>=` operators defines our evaluation strategy; it
allows us to combine computations. Be carefult of confusing
`m` (a type constructor) with the value constructors for a
> 3) The last two properties are not comparing values but
> functions. Right? For example, reading the second-last
> (m >>= return) :: a -> m a
Not quite -- `someMonadicValue >>= return` is of type `m a`.
> Where (m >>= return) is defined by (\x -> m x >>= return).
> Right? And this is why you can write the last property:
> (k >>= h) :: a -> h a defined by (\x -> k a >>= h) This is
> what allows us to make (k >>= h) the second argument of ">>="
> as in the last property:
Actually, the third law is:
(m >>= f) >>= g = m >>= (\x -> f x >>= g)
More information about the Beginners