;; 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-beginner-reader.ss" "lang")((modname lect21-scope) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require "let-for-bsl.rkt") ;;; From last time: what is 'let' good for? ;;; - break a large expression into (named) parts ;;; - avoid repeated computation ;;; ;;; (If a language doesn't have 'let', ;;; can we still get those benefits?) ;;; READING: Scott: Sections 3.1, 3.3 (excluding 3.3.{4,5}) ;;; [that's about 15pp total] (define x 7) (let {[x 4] [z (+ x 2)]} ; this is the "outer" x, 7. (+ x z)) ; this is the "inner" x, 4. (let* {[x 4] [z (+ x 2 (let {[x 99]} (* x x)))]} (+ x z)) (let* {[x 4] [y 19] [w (+ y x (let ([x 19]) (* x x)))] [z (+ x 2)]} (+ x z)) ;; In a program-text, an identifier-occurrence can be ;; a "binding occurrence" or a "applied occurrence" (?). ;; For every binding occurence, we should define its *scope*: ;; what region of the program-text corresponds to that binding occurrence? ;; (So that for every applied-occurrence, we should be able to answer: ;; "Where is the binding occurrence [if any]"?) ;; ;; We say "an identifier v is free within the expression e" if v occurs in e ;; but is not bound *within e*. ;; So x is free in (let {[z 5]} (+ x z)) ;; but z is not. ;; Note that x is *not* free inside the larger expression (let {[x 9]} (let {[z 5]} (+ x z))) ;; (For a variable to be free within an entire program is probably ;; an error 'undeclared variable'.) ;;; "alpha substitution" or "alpha renaming" -- ;;; lift to the top level, and re-name to not conflict. ;;; But only lift *free* occurrences in the expression. ;;; "beta reduction" -- re-write the body of a function, with params ;;; replaced by args (but only replace *free* occurrences in the body).