Now that we have environments with local bindings we can add
functions! In Lisp-like languages functions are values. What
does this mean? It means that along with values like 5 and
(1 2 3)
there are values that mean "add two numbers". These values can be put
into lists, passed around, whatever you want.
To call a function value in Scheme, you put it at the head of a list
that is evaluated. If we call the function that adds one to its argument
plusone, then we can call the function with an argument using the syntax
The new special form we're going to add is function application. If we are evaluating
a list and the first element is
begin, we have a special case for handling
that. If the first element is
quote, we have a different special case for
handling that. If no special cases apply, then the application form applies.
The syntax for application is
(_func _args...). In the interpreter,
we'll evaluate the first element of the list and get back a function value.
Then we'll evaluate the rest of the elements of the list. Finally
we'll call the function value with all the evaluated arguments as parameters.
For example, let's see what happens when we evaluate
in an environment that binds
plusone to a function value that adds
one to its input and
x to the value 7. Our
interpreter first looks at the head, sees that it is not a special form
quote. Then it evaluates
plusone in the current environment.
This is a variable lookup; it finds a function value. Then
x and gets 7. Finally it
calls the function value it got for
plusone with the value 7 as
argument, and returns the result, 8, as the evaluation of the expression.
That creates a function value and binds
f to the value. But we don't
We can call them with arguments:
Now we'll make an implementation choice. We'll decide that in our
as Scheem function values. That means that in our example with
the value bound to
plusone was the value:
Let's start by implementing application of functions with exactly one argument to keep things
as simple as possible. The syntax will be
(_func _arg). Remember
that for function application both
_arg are evaluated.
With function application with one argument,
your interpreter will be able to handle expressions like
(plusone (always3 10)),
which evaluates to 4.
Write a version of
evalScheem that can handle variable references
using the new environment model,
quote, and function application with one argument.
You can assume you have
lookup already defined.