Antti-Juhani Kaijanaho wrote:
[snip]
> I had many problems writing programs in the do notation until I understood
> the underlying (>>=).  Why?  For example, in imperative languages I
> can rewrite
> 
>          a <- b
>          c <- f a
> 
> as
> 
>         c <- f b
> 
> In Haskell I can't.  Why?  b is of type IO something, whereas f expects a
> non-monadic argument.  This confused me greatly, until I started thinking
> of do as syntactic sugar for (>>=).  Thus, while "do" does help someone
> who knows (>>=) to visualize the operations, you can never use do with
> plain imperative intutition: for example, you can't think of "a <- b"
> as assignment (which is the immediate analogy for someone who does not
> know monads well).  Therefore, even using "do" requires one to understand
> the play with types and actions as values, and from there it's trivial
> to get to (>>=).
It is indeed absolutely crucial to understand the difference between the
types "IO a" and "a" (an "IO a" is a way of obtaining an a, an "a" _is_
an a).  Also understand that in "do" expressions, "<-" represents actually
getting an "a" from an "IO a".  Then there is no confusion.  I think Antti-Juhani
Kaijanaho's confusion could have been resolved by thinking about the types
rather than about (>>=).


Reply via email to