madhukar93
madhukar93

Reputation: 515

why isn't this incorrect indentation in haskell?

Why doesn't line 5 contain an indentation error. I expected to get a parse error on compilation. I expected that the + on line 5 would have to be aligned under the * in line 4.

module Learn where

x = 10
  * 5
 + y  -- why isn't this incorrect indentation

myResult = x * 5

y = 10

Upvotes: 0

Views: 115

Answers (2)

glguy
glguy

Reputation: 1090

This compiles because the definition of x was all to the right of the beginning of x. It's not important where each line starts as long as those lines are indented to the right of x.

Upvotes: 1

chi
chi

Reputation: 116174

It compiles because there's no block there to consider.

Indentation only matters after where, let, do, case of. These keywords start a block of things, and it is important to understand whether a line continues the previous entry, starts a new entry, or ends the block.

case f 5 of
   A -> foo
      32         -- continues the previous entry
   B -> 12       -- starts a new entry
+ bar 43         -- ends the case

After = we do not need to split a block into entries: there's only a single expression. Hence no indentation rules apply.

Upvotes: 4

Related Questions