[Haskell-beginners] question on layout

Daniel Fischer daniel.is.fischer at web.de
Tue Jun 16 21:39:56 EDT 2009


Am Mittwoch 17 Juni 2009 01:41:01 schrieb George Huber:
> Hi,
>
> This is more of a philosophical question then anything else.
>
> Recently I posted a question (see " exercise 3.10 in YAHT") and now
> Rolle is encountering what appears to be the same error (see "hci
> reports "The last statement in a 'do'    construct must be an
> expression" error").  In both cases the responses have been something on
> the order of "must be the indentation".
>
> So...my questions are:
>
> (1) what was the driving force behind using white-space to denote code
> blocks?  From a beginners perspective (especially coming from a strong C
> / C++ background) this seems to add to the learning curve for the
> language, and can add a good deal of frustration.

Readability.
Properly indented code is far easier to read, braces and semicolons are then only clutter.

>
> (2) I know using layout is optional -- we can curly braces and
> semicolons (not sure what this method is called correctly), but in the
> books and sources that I have looked at this seems to be mentioned in
> passing, so I'm guessing "good haskell format" is to use layout.  And if
> this is true, is there any reason for this?  better performance? easier
> parsing?

It's only easier to parse for humans, it's harder to write a parser for the layout rule.
But humans are considered more important.

>
> (3) Finally, is there a book or online reference that uses curly braces
> and semicolons from the start -- maybe introducing the concept of layout
> after all the language syntax is firmly grounded?
>

I know none.

>
> George.
>
> PS -- For those of you that answered my first post, I sort-of solved my
> problem.  From your conviction that there was not an error in the
> function (even though this is where the compiler was claiming it to be),
> I moved stuff around in the source file while not touching the function
> itself and the error disappeared.  Not a satisfactory solution I know,
> but for now it will do till I get the syntax of Haskell down.  Thanks
> for everyone's help.

Read http://haskell.org/onlinereport/lexemes.html#sect2.7
and http://haskell.org/onlinereport/syntax-iso.html#sect9.3
for an explanation of the layout rule.

>
> George



More information about the Beginners mailing list