#lang racket ;;; READING: Scott: 3.1, 3.3 (excluding 3.3.{4,3}) ;;; [that's about 15pp total] (define x 7) (define y 99) ; For `let`: the scope of the introduced identifers is ; the *body* of the let, and nothing else. ; (let {[x 4] [z (+ x 2)]} ; this is the "outer" x, 7. (+ x y z)) ; this is the "inner" x, 4. ; For `let*`: the scope of the introduced identifiers is ; all following right-hand-sides, as well as the body of the `let*`. ; (let* {[x 4] [z (+ x 2)]} ; this is the "above" x, 4, for `let*`. (+ x y z)) ; this is the "inner" x, 4. #| Terms: (static) scope of a variable -- the part of a program where that variable can be used. e.g. in `{ for (int i=0; i<100; ++i) { int sumSoFar=0; sumSoFar += i; } return sumSoFar; } Shadowing -- when one variable has the same name as another, the scope of the inner one *shadows* the scope of the outer. "Binding occurrence" of a variable: where it is declared. "bound variable [within a region]": A variable is "bound within a region" if: the region contains that variable's binding occurrence. "free variable", or "unbound variable" [within a region]: not bound in that region. |# #| Consider in C: // A demo re scope/shadowing: // int foo( int n ) { { // a new block -- delimits scope. int n = n +1; // a new var; left-hand-side shadows the param. return n; } return n; } #include int main( int argc, char** args ) { printf("foo(7) returns %d.\n", foo(7)); return 0; } |#