Where are line breaks allowed within Haskell expressions?

7k views Asked by At

Background

Most style guides recommend keeping line lengths to 79 characters or less. In Haskell, indentation rules mean that expressions frequently need to be broken up with new lines.

Questions:

Within expressions, where is it legal to place a new line?

Is this documented somewhere?


Extended question: I see GHC formatting my code when it reports an error so someone has figured out how to automate the process of breaking long lines. Is there a utility that I can put haskell code into and have it spit that code back nicely formatted?

1

There are 1 answers

0
Norman Ramsey On BEST ANSWER

You can place a newline anywhere between lexical tokens of an expression. However, there are constraints about how much indentation may follow the newline. The easy rule of thumb is to indent the next line to start to the right of the line containing the expression. Beyond that, some style things:

  • If you are indenting an expression that appears in a definition name = expression, it's good style to indent to the right of the = sign.

  • If you are indenting an expression that appears on the right-hand side of a do binding or a list comprehension, it's good style to indent to the right of the <- sign.

The authoritative documentation is probably the Haskell 98 Report (Chapter 2 on lexical structure), but personally I don't find this material very easy to read.