Haskell/Indentation

Haskell relies on indentation to reduce the verbosity of your code. Despite some complexity in practice, there are really only a couple fundamental layout rules.

The golden rule of indentation
Code which is part of some expression should be indented further in than the beginning of that expression (even if the expression is not the leftmost element of the line).

The easiest example is a 'let' binding group. The equations binding the variables are part of the 'let' expression, and so should be indented further in than the beginning of the binding group: the 'let' keyword. When you start the expression on a separate line, you only need to indent by one space (although more than one space is also acceptable and may be clearer).

You may also place the first clause alongside the 'let' as long as you indent the rest to line up:

This tends to trip up a lot of beginners: All grouped expressions must be exactly aligned. On the first line, Haskell counts everything to the left of the expression as indent, even though it is not whitespace.

Here are some more examples:

Note that with 'case' it is less common to place the first subsidiary expression on the same line as the 'case' keyword (although it would still be valid code). Hence, the subsidiary expressions in a case expression tend to be indented only one step further than the 'case' line. Also note how we lined up the arrows here: this is purely aesthetic and is not counted as different layout; only indentation (i.e. whitespace beginning on the far-left edge) makes a difference to the interpretation of the layout.

Things get more complicated when the beginning of an expression is not at the start of a line. In this case, it's safe to just indent further than the line containing the expression's beginning. In the following example,  comes at the end of a line, so the subsequent parts of the expression simply need to be indented relative to the line that contains the , not relative to the   itself.

Here are some alternative layouts which all work:

Explicit characters in place of indentation
Indentation is actually optional if you instead use semicolons and curly braces for grouping and separation, as in "one-dimensional" languages like C. Even though the consensus among Haskell programmers is that meaningful indentation leads to better-looking code, understanding how to convert from one style to the other can help understand the indentation rules. The entire layout process can be summed up in three translation rules (plus a fourth one that doesn't come up very often):


 * 1) If you see one of the layout keywords,, insert an open curly brace (right before the stuff that follows it)
 * 2) If you see something indented to the SAME level, insert a semicolon
 * 3) If you see something indented LESS, insert a closing curly brace
 * 4) If you see something unexpected in a list, like , insert a closing brace before instead of a semicolon.

For instance, this definition...

...can be rewritten without caring about the indentation rules as:

One circumstance in which explicit braces and semicolons can be convenient is when writing one-liners in GHCi:

Indent to the first
Due to the "golden rule of indentation" described above, a curly brace within a  block depends not on the   itself but the thing that immediately follows it. For example, this weird-looking block of code is totally acceptable:

As a result, you could also write combined if/do combination like this:

It isn't about the, it's about lining up all the items that are at the same level within the.

Thus, all of the following are acceptable: or or