[Haskell-cafe] Re: if - then - else layout

Achim Schneider barsoap at web.de
Thu Sep 25 05:12:26 EDT 2008

Jules Bean <jules at jellybean.co.uk> wrote:

> 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".
> 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.
There's no need to hack the layout rule, you're even giving pointers to
the solution. Something like this:

if p = do
	(_, c, a) <- get
	put (b, c, a)

then c = do
	(b, _, a) <- get
	put (b, c, a)

else a = do
	(b, c, _) <- get
	put (b, c, a)

end = do
	(b, c, a) <- get
	return if p then a else c

Advantages are obvious: Order doesn't really matter anymore, as in

then "get away"
else "or else"
if i tell you to

Furthermore, this scheme supports logical comments, a rare kind of
control structure enabling mindboggingly diverse rapid prototyping

if i knew what i want to do
if i knew how to do it
then i'd have written the next line much earlier
if i wrote this line
then i don't need to remove the other lines
else where in other languages i'd have to do that

(c) this sig last receiving data processing entity. Inspect headers
for copyright history. All rights reserved. Copying, hiring, renting,
performance and/or broadcasting of this signature prohibited.

More information about the Haskell-Cafe mailing list