Lecture 12: The Scope of Things



  1. Recall that functions may process data in parallel:

    Project: Define the function merge, which consumes two sorted lists of numbers and produces a single sorted list of numbers w/o duplicates. (see lecture 11


  2. Compare
    (define (f x) (+ (* 3 x x) (* 2 x) 7))
    
    (f 5)
     = (+ (* 3 5 5) (* 2 5) 7)
     = (+ 75 10 7)
     = ... 
    
    and
    (define (f y) (+ (* 3 y y) (* 2 y) 7))
    
    (f 5)
     = (+ (* 3 5 5) (* 2 5) 7)
     = (+ 75 10 7)
     = ... 
    

    Why did it not matter that we wrote f with x as the parameter and then f with y?

    A placeholder stands for a value. After we replace it with its value, it's useless. The only thing that matters is that we use x when we want to refer to the input position (parameter) x.

    Terminology: We say that the all occurrences of x in the body of f are bound to the parameter (aka binding) position of f:

    (define (f x) (+ (* 3 x x) (* 2 x) 7))
               |          ^ ^       ^
               |          | |       |
                --------------------
    
    The direction of the arrow suggests that the value flows from the parameter positions to the bound occurrences.

    Terminology: We say that the scope of x is the body of f and that we can rename x to y in its scope. So in

    (define (f x) (+ (* 3 x x) (* 2 x) 7))
               |          ^ ^       ^
               |          | |       |
                --------------------
    
    (define (g x) (* x x))
    
    the x of f and the x of g have nothing to do with each other.
  3. What is the scope of f? It is the entire Definition window and Evaluation window in DrScheme. If we rename it, we rename it everywhere, right?

    Well almost. The scope of f can have a hole:

    (define (f x) (+ (* 3 x x) (* 2 x) 7))
    
    (define (g f) (* f f))
               |     ^ ^
               |     | |
                -------
    
    (define (h y) (f (+ y y)))
    

    A renaming in action of f to fff may only result in:

    (define (fff x) (+ (* 3 x x) (* 2 x) 7))
    
    (define (g f) (* f f))
               |     ^ ^
               |     | |
                -------
    
    (define (h y) (fff (+ y y)))
    
    Draw arrows for fff.
  4. A new expression: local. It is not needed per se but it is useful to combine several functions into one and to name intermediate results

    When we discuss new constructs, we must discuss

    1. their grammatical rules
           expression is either 
      	- ... 
      	- ... or: 
      	- (local (... definitions ...) expression)
      
           definition is either 
      	- (define (name name ...) expression)
      	- (define name expression)
      	- (define-struct name (name ...))
           


      Example:

       
              (define (f x)
                (local ((define (square y) (* y y)))
                  (* pi (square x))))
           
    2. their scoping rules: definitions in local are like definitions in the Definitions window BUT their scope extends only from the left paren to the right paren

    3. the evaluation rule:
       
            Definitions: 
              (define (f x)
                (local ((define (square y) (* y y)))
                  (* pi (square x))))
            -----------------------------------------
            Evaluations: 
              (f 1)
            = (local ((define (square y) (* y y)))
                (* pi (square 1)))
            = (* pi (square 1)) + (define (square y) (* y y)) added to definitions
           
      So now act as if square were a part of the definitions:
       
            Definitions: 
              (define (f x)
                (local ((define (square y) (* y y)))
                  (* pi (square x))))
      
              (define (square y) (* y y))
            -----------------------------------------
            Evaluations: 
              (* pi (square 1)) 
            = (* pi (* 1 1))
            = 3.14....
           

      These rules are approximate and partially flawed. They will need an amendment.

  5. A pragmatic example: if time permits: insertion sort
     
    (define (sort alon)
      (cond ...))
    
    (define (insert alon)
      ...)
    
    edit and revise:
     
    (define (sort alon)
      (local ((define (sort alon)
    	    (cond ...))
    
    	  (define (insert alon)
    	    ...))
        (sort alon)))
    
    Scope! Eval!




Matthias Felleisen This page was generated on Fri Apr 9 09:17:38 CDT 1999.