Buckets of Marbles

Remember Figure 2 from back in Chapter 2?

Colored Scope Bubbles

Fig. 2 (Ch. 2): Colored Scope Bubbles

This exercise asks you to write a program—any program!—that contains nested functions and block scopes, which satisfies these constraints:

  • If you color all the scopes (including the global scope!) different colors, you need at least six colors. Make sure to add a code comment labeling each scope with its color.

    BONUS: identify any implied scopes your code may have.

  • Each scope has at least one identifier.

  • Contains at least two function scopes and at least two block scopes.

  • At least one variable from an outer scope must be shadowed by a nested scope variable (see Chapter 3).

  • At least one variable reference must resolve to a variable declaration at least two levels higher in the scope chain.

TIP:
You can just write junk foo/bar/baz-type code for this exercise, but I suggest you try to come up with some sort of non-trivial real’ish code that at least does something kind of reasonable.

Try the exercise for yourself, then check out the suggested solution at the end of this appendix.