Comp 210 Lab 7: Local & Scope

Index: What is local?, Scope, When to use local?, When not to use local?, local & the Design Methodology

First, to use local, change DrScheme's language level to "Intermediate Student".


What is local? (Recap)

Class introduced local. Let's start with a quick review. Its syntax is

     (local [definitions] expression)
for example,
     (local [(define x 3)
             (define y (+ x 1))]
        (+ x y))

How do we evaluate it? Basically, we

  1. evaluate the definitions' expressions, in sequence,
  2. temporarily make the resulting definitions global, and
  3. evaluate the local's expression body.
There is one problem with this: the locally defined names may conflict with previous definitions. How do we disambiguate which definitions are being referred to? One way to explain this is that we'll rename the local variables consistently, something like x to x', so that the resulting names have not been used before. So, we amend the previous evaluation strategy to
  1. rename the locally defined variables consistently in both the definitions' expressions and the local's expression body,
  2. evaluate the definitions' expressions, in sequence,
  3. temporarily make the resulting definitions global, and
  4. evaluate the local's expression body.

local does not allow you to solve any problems that you couldn't solve before, but it does allow you to solve them with better programs.

To do: Hand-evaluate all steps of the following program:

     (define x 3)
     (define z 6)
     (local [(define x 7)
             (define y (+ x 4))]
        (+ x y z))


Scope

Previously, in programs like

     (define x 3)
     (define (square x)
        (* x x))
     (square 4)
we knew that the variable x inside square was somehow different from the one outside. We didn't discuss this much, since it was fairly intuitive. Our terminology is that the x inside the function is local, while the other is global. We also say that the local variable shadows (or hides or masks) the identically-named global variable.

This distinction is one of scope. I.e., we can have multiple distinct variables with the same name. Each variable has a scope -- the part of the code where that variable can be referred to. Previously, we variables either had local scope or global scope. Now, local allows us to make further distinctions. I.e., some variables will be "more local" than others.

Thinking about scope is basically a shortcut to the whole renaming step of local evaluation. Once you understand scope, you simply say that a use of variable x refers to the most local definition of x.

The scoping rules are rather simple:

That's a lot of words for an idea that most people find reasonably intuitive. Really the main difference now is that we have a mechanism for nesting definitions, so we can have more than just the two levels of "global" and "local" variables.

If you use DrScheme's "Check Syntax" button, it will show you the scope of variables:

To do: Use DrScheme's "Check Syntax" with the previous examples.

To do: The following are stylistically ugly toy examples for scoping. Draw arrows between definition and uses in the following examples, and figure out what are the results of the expressions. Check your answers with DrScheme's "Check Syntax" button and its evaluation.

(define x 1)
(define y 2)
(define z 3)

(define (fee x y)
   (local [(define z 7)
           (define y 4)]
      (+ x y z)))
(fee x z)

(define (fie x y)
   (local [(define z 7)
           (define y (+ y 3))]
      (+ x y z)))
(fie x z)

(define (foe x y)
   (local [(define z 7)
           (define y (+ x z))
           (define x (local ((define y 10)) (+ y z)))]
      (+ x y z)))
(foe x z)

(define (fum x y)
   (local [(define fum 7)
           (define (x z) (+ y z))]
      (x fum)))
(fum x z)

(define (foo x y)
   (local [(define (z y)
               (cond [(zero? y) 1]
                     [(positive? y) (* x (z (sub1 y)))]))]
      (local [(define x (+ y 1))]
         (+ x (z y)))))
(foo x z)

Fortunately, real-world examples as convoluted as these are uncommon. However, the last example's use of x in z is a common technique we'll see more of later.


When to use local?

There are several overlapping reasons for using local:

To do: Go back and review your last two assignments. Use local where appropriate.

You are expected to use local on the current and future assignments where appropriate.

For the curious... You can also use define-struct in a local. Q: When would this be appropriate?


When not to use local?


local & the Design Methodology

You should still use the design methodology for developing local functions. In particular, local functions still need a contract and purpose.

However, there are complications. You can't test a local function independently, because it is hidden. A standard technique is to define the function globally for testing, then move it into a local. This techniques doesn't work when the function uses variables that aren't local to the function, as when eliding invariant arguments, e.g.,

(define (expt x y)
   (local [(define (expt-of-x y)
               (cond
                   [(zero? y)     1]
                   [(positive? y) (* x (expt-of-x (sub1 y)))]))]
      (expt-of-x y)))