The goal of this section is to extend the evaluator of
section
so that it can cope with function
applications and function definitions. In other words, the new evaluator
simulates what happens in DrScheme when we enter an expression in the
Interactions
window after clicking Execute. To make
things simple, we assume that all functions in the
Definitions
window consume one argument.
Exercise 17.7.1
Extend the data definition of exercise
so that we can
represent the application of a user-defined function to an expression such
as (f (+ 1 1)) or (* 3 (g 2)).
The application should be
represented as a structure with two fields. The first field contains the
name of the function, the second one the representation of the argument
expression. Solution
A full-fledged evaluator can also deal with function definitions.
Exercise 17.7.2
Provide a structure definition and a data definition for definitions. Recall that a function definition has three essential attributes:
Translate the following definitions into Scheme values:
Exercise 17.7.3
Develop evaluate-with-one-def. The function consumes (the representation of) a Scheme expression and (the representation of) a single function definition, P.
The remaining expressions from exercise
are evaluated
as before. For (the representation of) a variable, the function signals an
error. For an application of the function P,
evaluate-with-one-def
(evaluate-with-one-def (subst ... ... ...)
a-fun-def)
Exercise 17.7.4
Develop the function evaluate-with-defs. The function consumes (the representation of) a Scheme expression and a list of (representations of) function definitions, defs. The function produces the number that DrScheme would produce if we were to evaluate the actual Scheme expression in the Interactions window and if the Definitions window contained the actual definitions.
The remaining expressions from exercise
are evaluated
as before. For an application of the function P,
evaluate-with-defs