[Haskell-cafe] if - then - else layout

Jules Bean jules at jellybean.co.uk
Thu Sep 25 04:45:12 EDT 2008


leledumbo wrote:
> consider this partial program:
> if n>5 then
>   putStrLn "big"
> else
>   putStrLn "small"
> 
> this works fine in hugs, but in ghc I must change it to:
> if n>5
>   then
>     putStrLn "big"
>   else
>     putStrLn "small"

Actually both of those are valid expressions.

And they both work in hugs and ghc.

The question I imagine you're asking involves layout mode:

do
   if n>5 then
     putStrLn "big"
   else
     putStrLn "small"

this is shorthand for

do { if n > 5 then putStrLn "big" ; else putStrLn "small" }

which is a syntax error. A statement in a do block cannot begin with the 
keyword "else".

If you indent the else a bit further than it counts and a continuation 
of the enclosing expression (beginning with if) so it desugars to

do { if n > 5 then putStrLn "big" else putStrLn "small" }

which is fine.

Haskell' is apparently going to include a hack to permit this case. I 
think that's a poor decision, because including a hack to the layout 
rule makes it harder to understand and explain the layout rule.

Jules


More information about the Haskell-Cafe mailing list