Hints for Writing shadowed-vars
Introduction
The shadowed-vars problem is a bit bigger than the
usual homework problem, including is-declared?.
Like lexical-address, it benefits from you using
several different tools you've learned over the semester. You
will probably also want to create a helper function or two.
Below are hints to move you in the right direction. Feel free to use any or all of them.
Function Structure
Use structural recursion over the inductive expression type to define the structure of your function. There are four kinds of expression in the core language, so your function will make a four-way choice.
Return Value
(shadowed-vars exp) returns a list. That means
all four cases have to return a list.
The Variable Reference Case
There are no variables shadowed in a variable reference, obviously, so that case returns an empty list.
The if Expression Case
In an if expression, there could be shadowed
variables in any of the three parts. Use recursive calls to
find them, and then append the three results.
append
is
a variable-arity function,
so you can combine the three lists with one call.
The Application Case
In an app expression, there could be shadowed variables in the procedure part or in any of the arguments. Find the answer for the procedure part and the answer for the arguments, and then append the results.
You can find the answer for the arguments in one of two ways:
-
Use
the map-reduce pattern
to compute a list of lists of shadowed variable, then reduce
them to a single list with
append. - Write a recursive helper function that walks through the arguments one at a time and builds the answer one list at a time. The helper will be processing a list of symbols much as we have many times in the past.
The lambda Case
In a lambda expression, a variable can be shadowed
in one of two ways:
-
it is shadowed in the
lambda's body, or -
it is one of the
lambda's parameters and is declared in the body.
Compute these two lists and append the results.
Again, you can find the answer for the lambda's parameters using the map-reduce pattern or by writing a recursive helper function. For this one, you may find the recursive helper function a bit easier to write and understand.