Function values

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 (plusone 2).

Application

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 (plusone x) 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 like begin or quote. Then it evaluates plusone in the current environment. This is a variable lookup; it finds a function value. Then it evaluates 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.

JavaScript function values

JavaScript has function values as well. That's what we've been using when we do things like:

That creates a function value and binds f to the value. But we don't have to use function values in JavaScript that way. We can put them in a list:

We can call them with arguments:

Scheem function values are JavaScript function values

Now we'll make an implementation choice. We'll decide that in our implementation of Scheem, we'll use JavaScript function values as Scheem function values. That means that in our example with plusone, the value bound to plusone was the value:

Implement application

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 _func and _arg are evaluated.

The tests use an environment that defines a few different JavaScript functions that take one argument.

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.

1:1



Prev Next