4 ms·
The chapter raises the question of the semantics of { var a = "outer"; { var a = a; } } and { var a = a; } .
by TheAsprngHacker 8y ago
The chapter raises the question of the semantics of
{
var a = "outer";
{
var a = a;
}
}
and
{
var a = a;
}
. Some programming languages have the idea of "let rec" versus "nonrecursive let." In Haskell, `let` is recursive, and because Haskell is lazy, `let a = a` is a nonterminating computation. In OCaml, `let` is non-recursive and `let rec` is recursive, and only certain recursive definitions are allowed. (You can do `let rec a = 0::a`, but not `let rec a = a`.)
Scheme has `let`, where none of the variables are in scope for any definition, `let*`, where each variable is in scope in the successive definitions, and `letrec`, where all of the variable are in scope for all definitions. According to http://www.r6rs.org/final/r6rs.pdf http://www.r6rs.org/final/r6rs.pdf, Scheme initializes letrec'd variables to a "black hole."
- AnaniasAnanas 8y ago> and because Haskell is lazy, `let a = a` is a nonterminating computation I am pretty sure that lazyness has nothing to do with this.
- TheAsprngHacker 8y agoMaybe I worded that wrong. In OCaml and other strict languages, `let (rec) a = a` doesn't make sense, because `a` doesn't have a value when you are defining it to be itself. When I write `let a = a` in Haskell, what happens is that `a` is set to a thunk whose code pointer points to its own evaluation code. When the thunk gets forced, it tries to evaluate itself. Doing my research, I was wrong about the "nonterminating computation" part; when the thunk gets forced, it gets marked as a "black hole," detecting the infinite recursion in this case.
- z3t4 8y agoI think functional scope was a step forward from block scope, so I got very sad when block scope got into JavaScript and is now the standard. JS still allow you to use function scope, and I still do, but most tutorials now use block scope. With function scope I can declare variables where they are used, they will be automatically hoisted. This also solves the issue with using a variable before they are declared ... you declare variables where they are used. Function scope also encourages the use of functions and closures, which makes (async) programing more fun.