14

In a Haskell tutorial I ran across the following code:

do [...]
  let atom = [first] ++ rest
  return $ case atom of

Note that the let expression does not have an in block. What is the scope of such a let expression? The next line?

mdm
  • 5,528
  • 5
  • 29
  • 28

2 Answers2

15

Simply put, it's scoped "from where it's written until the end of the do".

Note that within a do statement, let is handled differently.

According to http://www.haskell.org/haskellwiki/Monads_as_computation#Do_notation , it is interpreted as follows:

do { let <decls> ; <stmts> }
  = let <decls> in do { <stmts> }
Deestan
  • 16,738
  • 4
  • 32
  • 48
8

The scope is the rest of the do block.

See §3.14 of the Haskell Report (specifically, the fourth case in the translation block). (Yes, this is the section about do blocks, because let without in is only valid inside a do block, as Porges points out.)

dave4420
  • 46,404
  • 6
  • 118
  • 152