Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are line breaks allowed within Haskell expressions?

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?

like image 275
John F. Miller Avatar asked Jun 12 '15 19:06

John F. Miller


People also ask

How do you skip a line in Haskell?

To create a string containing a newline, just write "\n" .

How do you break a code line?

The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.

How do you represent a line break in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


1 Answers

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.

like image 173
Norman Ramsey Avatar answered Sep 20 '22 22:09

Norman Ramsey