Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of let when used without in?

Tags:

let

haskell

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?

like image 589
mdm Avatar asked Mar 19 '12 20:03

mdm


People also ask

What is the scope of let keyword?

Description. let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.

Is let function scoped?

let is block-scoped Hence when you try to access b outside of if block, an error occurs (as shown above in the program). Note: The variables declared inside a function will be function scoped for both var and let .

Is let global scoped?

Summary. Variables are declared using the let keyword are block-scoped, are not initialized to any value, and are not attached to the global object.

What is the scope of LET and const?

The scope of a let variable is block scope. The scope of a const variable is block scope. It can be updated and re-declared into the scope. It can be updated but cannot be re-declared into the scope.


2 Answers

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> }
like image 66
Deestan Avatar answered Sep 25 '22 23:09

Deestan


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.)

like image 21
dave4420 Avatar answered Sep 25 '22 23:09

dave4420