[Haskell-cafe] Problems with do notation

Luke Palmer lrpalmer at gmail.com
Thu Nov 22 03:35:02 EST 2007


On Nov 22, 2007 8:19 AM, Peter Verswyvelen <bf3 at telenet.be> wrote:
>  worksFine =
>    if True
>    then putStrLn "True"
>    else putStrLn "False"

This is just an expression, the indentation is inconsequential.

>  worksNOT = do
>    if True
>    then putStrLn "True"
>    else putStrLn "False"

The first line, "if True", sets the indentation level of the
statements in the "do" to two spaces.  So this is interpreted as

  worksNOT = do {
    if True ;
    then putStrLn "True" ;
    else putStrLn "False"
  }

Which is of course illegal.

>  worksAgain = do
>    if True
>      then putStrLn "True"
>      else putStrLn "False"

Here, the indentation level of the "do" is still two spaces, but then
then and else are at a higher indent than that, so they are
interpreted as part of the preceding expression.  The rules are
actually very simple.

Luke


More information about the Haskell-Cafe mailing list