In our definitions of
lend2, the left margin of our text wandered
around quite a bit. This was not an accident; in Haskell, whitespace has
Haskell uses indentation as a cue to parse sections of code. This use of layout to convey structure is sometimes called the offside rule. At the beginning of a source file, the first top-level declaration or definition can start in any column, and the Haskell compiler or interpreter remembers that indentation level. Every subsequent top-level declaration must have the same indentation.
Here’s an illustration of the top-level indentation rule; our first file, GoodIndent.hs, is well-behaved:
-- file: ch03/GoodIndent.hs -- This is the leftmost column. -- It's fine for top-level declarations to start in any column... firstGoodIndentation = 1 -- ...provided all subsequent declarations do, too! secondGoodIndentation = 2
Our second, BadIndent.hs, doesn’t play by the rules:
-- file: ch03/BadIndent.hs -- This is the leftmost column. -- Our first declaration is in column 4. firstBadIndentation = 1 -- Our second is left of the first, which is illegal! secondBadIndentation = 2
Here’s what happens when we try to load the two files into ghci:
:load GoodIndent.hs[1 of 1] Compiling Main ( GoodIndent.hs, interpreted ) Ok, modules loaded: Main.
:load BadIndent.hs[1 of 1] Compiling Main ( BadIndent.hs, interpreted ) BadIndent.hs:8:2: parse error on input `secondBadIndentation' Failed, modules ...