[Haskell-cafe] About do notation.

Thomas M. DuBuisson thomas.dubuisson at gmail.com
Wed Oct 15 03:26:51 EDT 2008

Magicloud wrote:
> Hi,
>     As some articles say, do notation is expand to (>>) and (>>=)
> when 
> being compiled.
>     So I want to know the details. Like:
> main = do
>   a <- getArgs
>   b <- getLine
>   myFunc1 (head a) b
>   myFunc2 b (head a)
>     I cannot figure out what is the (>>) and (>>=) way of this.
> Thanks.

In the beginning, their was the bind (>>=) operator and lambdas.  You
bind the results of one action to the argument of the next
	(act >>= \a -> act2 a)

A genious realized that not all actions have an intersting result, so
instead of a meaningless binding (act >>= \_ -> act2) you can sequence
the actions (act >> act2).

So code would look like:
main =
  getArgs >>= \a -> getLine >>= \b ->
  myFunc1 (head a) b >>
  myFunc2 b (head a)

It didn't take too long and people got in the habbit of lining up the
binding on the right-most column:

main =
  getArgs 		>>= \a ->
  getLine 		>>= \b ->
  myFunc1 (head a) b 	>>
  myFunc2 b (head a)

And eventually someone figured out this looked an aweful lot like
imparitive code with the variable and function flipped around (and some
ugly characters inbetween).
    getArgs >>= \a ->
    a = getArgs();

So the shortest most non-descript word, 'do', was found to ensure no one
would have to type much.


* Story not historically accurate.  Technical accuracy is questionable.
Sanity of author not guarenteed.  All rights reserved.  No refunds.

More information about the Haskell-Cafe mailing list