[Haskell-beginners] monads / do syntax

Thiago Negri evohunz at gmail.com
Tue Sep 4 18:38:42 CEST 2012


Go line by line, except the last one:
a) When you see the pattern "a <- b", just swap it with "b >>= \s ->";
b) When you see the pattern "b", put a ">>" at the end of the line.

In your case:

| h = (return 1 :: IO Integer) >>= \a ->
|    return 2 >>= \b ->
|    return (a + b + 1)


An example when you have a line without an assignment ("<-"), this:

| h = do a <- (return 1 :: IO Integer)
|        b <- (return 2)
|        putStrLn "Hello, world!"
|        return (a + b + 1)

Turns into:

| h = (return 1 :: IO Integer) >>= \a ->
|    return 2 >>= \b ->
|    putStrLn "Hello, world!" >>
|    return (a + b + 1)


2012/9/4 Christopher Howard <christopher.howard at frigidcode.com>:
> What does the following do expression translate into? (I.e., using >>=
> operator and lambda functions.)
>
> code:
> --------
> h = do a <- (return 1 :: IO Integer)
>        b <- (return 2)
>        return (a + b + 1)
> --------



More information about the Beginners mailing list