Title: Scope: Whats in a Name
1ScopeWhats in a Name?
- CMSC 11500
- Introduction to Computer Programming
- October 16, 2002
2Roadmap
- Recap Procedural Abstraction
- Procedures as Parameters scale
- Unnamed Procedures Lambda
- Scope
- Bound and free variables
- Local variables
- With formal parameters
- With let
- Procedures as return values
3Procedural Abstraction
- Goal single point of control
- Avoid copying and modifying code
- Identify similarities in procedures
- E.g. differ only in some function
- Solution Abstract, and pass differing
procedure(s) as parameter - Example map
- Doublelist, squarelist, etc,...
4Scalelist (before abstraction)
(define (scalelist scale alon) (define
(scale-fn x) ( x scale)) (cond ((null?
alon) ()) (else (cons (scale-fn (car
alon)) (scalelist (cdr alon)))))
- Observation1 Same pattern as squarelist, map
- Observation2 Defining scale-fn is silly
- Observation3 Where does scale get its value?
5Scalelist (after abstraction)
(define (scalelist scale alon) (define
(scale-fn x) ( scale x))
(map scale-fn alon))
6Unnamed Functions Lambda
- Issue Defining lots of trivial procedures
- E.g. add2, double, triple
- Solution unnamed function
- Cant be recursive
- (lambda (ltpar1gtltpar2gt..ltparngt) exp)
- E.g. Add2 (lambda (x) ( x 2))
- Apply like other procedures
- ((lambda (x) ( x 2)) 10) -gt 12
7Redefining with Lambda
(define (doublelist alon) (map (lambda (x)
( x x)) alon)) (define (triplelist alon)
(map ???? alon)) (define (scalelist scale alon)
(map ???? alon))
8Abstraction with Sum Product
(define (sum alist) (cond ((null? alist) 0)
(else ( (car alist) (sum (cdr alist))))
- Product?
- General Pattern?
9Observation 3 Value of Scale
(define (scalelist scale alon) (define
(scale-fn x) ( scale x))
(map scale-fn alon))
- Lexical scope
- Variable binding Parameters
- Binding occurrence Formal parameter of procedure
BINDS to value in procedure call - Bound occurrence Appearances of variable inside
procedure defn get value - free variable Global scope top-level define
10Scope
- Region where binding gives value
- Nearest enclosing block with binding occurrence
- Renaming
- Consistently rename variable w/in scope
- Meaning is unchanged
- (define (square x) ( x x))
- (define (double x) ( x x))
- (define (double y) ( y y))
- No change in meaning no effect on square..
11Scope Examples
(define (p1 x y) ( ( x y) ( ( 2
x) ( ( 2 y) 22)))) (define (p2 x) ( ( 55
x) ( x 11))) (define (p3 x) ( (p1 x 0)
( (p1 x 1) (p2 x))))
12Defining Local Variables with Let
- Local variables
- Store and name partial result
- Avoid repeated computation if result reused
- Make code more intelligible
(let ((x 3) (y 4)) ( x y))
12
13Let example
- Reverse-and-double
- ((a b) (c d)) -gt((b a) (b a) (d c) (d c) )
(define (reverse-and-double alist) (cond
((null? alist) ()) (else (cons (reverse (car
alist)) (cons (reverse (car
alist)) (reverse-and-double (cdr alist)))))
Problems?????
14Let Example
(define (reverse-and-double alist) (cond
((null? alist) ()) (else (let
((reverse-first (reverse (car alist)) (reverse-
rest (reverse-and-double (cdr alist))))
(cons (reverse-first (cons (reverse-first
reverse-rest))))
15Procedures as First-class Objects
- Can be
- Named by variables
- Can be passed as arguments to functions
- Can be included in data structures
- Can be returned as the results of functions
16Procedures as Results of Functions
- Class of procedures
- E.g. scale1, scale2, scale3 ---gt scale-n
(define (scale-n scaler) (lambda (x) ( x
scaler))) (define scale3 (scale-n 3)) (scalelist
scale3 (1 2 3 4)) ----gt (3 6 9 12)
17Summary
- Unnamed procedures
- Scope
- Variable binding
- Parameters as binding instances
- Naming local variables with let
- Returning procedures as results
18Next Time