CS 342 Lecture -*- Outline -*- * Scope rules ** expressions (statements) and scopes next hierarchically statements can contain statements (put up general contour diagram for Algol 60 program) *** motivation in FORTRAN, storage allocated before run program leads to waste or reuse in dangerous ways (COMMON) in Algol 60, storage allocated only for each procedure while running then reclaimed (stack) allows abstraction no need to worry about interference among procedures for var names. ** Static vs. dynamic properties static is before run-time (e.g., compile time) dynamic is run-time ** Declarations and uses *** static (lexical) scoping procedures called in environment of def. crucial for making closures be read as rules where they are defined ------------ (set s 10) (set f (lambda (x) (+ x s))) (set g (lambda (s) (f (+ s 11)))) (g 5) ;Value: 26 ------------ *** dynamic scoping (fluid binding) procedures called in environment of caller. used in LISP 1.5, Unix (for shell procedures), (real) APL example on p.134 (as above), and top of 135 main problem is scope for "upward funargs" functions returned by procs e.g., add1 in middle of p. 135 conclusion: dynamic scoping largely a mistake easy to make if you write your own interpreter...