Lexical and Dynamic Scope

What does "scope" mean, anyway? Why haven't we had to deal with it until now?

Let's think back to what happens when you call a procedure:

  1. Evaluate the arguments.
  2. Create a new frame and attach it to the procedure's right-bubble environment.
  3. Evaluate the procedure body in that new environment.

A very common mistake is to create a new frame and attach it to the current environment. Scheme doesn't work that way, but it turns out other languages do! (Like Logo.)

In other words, we have a choice:

How are you going to remember this? Well, normally (in Scheme), you can tell what variables a procedure has access to just by looking at where it's defined. "What variables it can access" is the same as "what environment it's in"! So in lexical scope, we can see environments in the text (code) of the program.

As for dynamic scope, you can't tell what the parent environment will be until the program runs. And "dynamic" means "moving" or "active"!

Thinking about what things mean...what we've been calling the "right-bubble environment" is actually "the environment this procedure is from", i.e. the one it was created in. That's important for things like make-adder, where we want to remember the value we're adding, not get a new value every time. So lexical scope means extending the environment the procedure came from.

A good way to check what environment you're in is to look at the code and say "what would the value of x be here?". Remember, lexical scope is what we've been working with all semester; it behaves exactly how you're used to.

Flat Scope

This is an "extra" topic; you're not responsible for it, but it's interesting!

If you think about, there's actually another natural choice for what environment you extend when you call a procedure:

Flat scope isn't very powerful, but it's very easy to understand: either a variable's defined in the current frame, in the global frame, or not at all. It's what's used in languages like C (which you'll use in 61C), because it's easy.

But this is not exactly true anymore! Both C++0x "lambdas" and Apple's "blocks" are implementations of lambda / closures / higher-order procedures in C, and they're already supported in some of the most popular compilers.

Back to Jordy's home page | Course home page