简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM