;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname scope-let-let*-examples) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;; NOTE TO SELF: discuss python's three scopes: ;; default: is function-local [and not any sub-functions]; ;; global [must be imported], ;; `nonlocal` to inherit scope from the one,surrounding function ;; (you can't use `nonlocal` inside a top-level function to get a global; ;; you can't use `nonlocal` inside a deeply-nested function to get ;; something declared more than one level above you.) ;; ;; Separately: ;; python's scope of variables using `:=` https://peps.python.org/pep-0572/#scope-of-the-target ;; Java16's scope of vars introduced in case/switch matching (define (foo x) (+ x b)) (define b 99) (define a (sqrt b)) (cons b (let {[a (+ a b)]} (let {[b (+ a b)]} (let {[c (+ a b)]} (cons a (cons b (cons c '()))))))) (cons b (let* {[a (+ a b)] [b (+ a b)] [b (+ b 99)] [c (+ a b)]} (cons a (cons b (cons c '()))))) #| class Foo { int n; void helper() { print(n); int n = n+2; print(n); } } |# ;; Def'n [Scott, Sect.3.3]: Scope: ;; The textual region of a program in which a binding is active. ; ; scope - where can a var be "seen" ; one var may "shadow" another [explain] ; ; binding occurence: declaring a new var ; bound occurrence: using a particular var. ; (show arrows in drracket) ; Syntax of `let`: #;(let { ... } ) ; syntax for ``: ; [ ] ; WHY doe we use variables? ; (1) avoid repeated computations ; (2) give meaningful names to sub-computations that will lead to my overall result (cons b (let {[a 15] [b (+ 2 3)] [c (* a b)]} (cons a (cons b (cons c '()))))) (cons b (let {[a 15] [b (+ 2 3)] } (let {[c (* a b)]} (cons a (cons b (cons c '())))))) (cons b (let {[a 15] [b (+ 2 3)]} (let {[c (* a b)]} (cons a (cons b (cons c '()))))))