[Haskell-cafe] Haskell newbie indentation query.

Daniel Fischer daniel.is.fischer at web.de
Wed Oct 15 08:22:00 EDT 2008


Am Mittwoch, 15. Oktober 2008 13:39 schrieb Ramaswamy, Vivek:
> Hello All~
>
> I have just started with Haskell, and I must confess; I am in love with
> it.
> However one area that I am really confused about is indentation.
> Lets take a look at if-else if- else block.
>
> The way I understand it:
> {------}
> if something
>  then do
>   something 1
>   something2
> else if nothing
>  then do
>   something3
>   something4
> else do
>  different
> {-------}
> The code above gives out an error.  I have been programming in python
> and the above appears fine.
> But it does not work.
> What works is:
> if something
>  then do
>   something1
>   something2
>   else if
>    then do
>     something3
>     something4
>     else do
>      different
>
> I find the above scheme extremely confusing. I tried going to:

Not for long, it'll become natural pretty fast.

>
> http://en.wikibooks.org/wiki/Programming:Haskell_indentation after
> reading I am even more confused.
> Can somebody please explain how the Haskell indentation works?
>
> The else-if and else seem to be aligning up with "then". They should be
> aligning with "If" in my opinion.

The "then ..." and the "else ..." branches are both part of the if-expression, 
so they have to be indented further than the "if". Aligning something with 
the "if" ends the expression, so if the "else" is aligned with the "if", 
there's an incomplete if-expression and something which should be an 
expression but isn't because it begins with "else". 

The Layout rule is explained in the report: http://haskell.org/onlinereport/
informally in section 2.7, formally in section 9.3, perhaps that helps.

>
> Thanks in advance.
>
>
> Regards
> -Vivek Ramaswamy-



More information about the Haskell-Cafe mailing list