Homework

Following your answers to the exercises, you'll add lambda and general function application to your Scheem interpreter.

Environments

First update the way environments are handled to the new way throughout your existing interpreter. Update define, set!, and variable references. Use your lookup, update, and add_binding functions. Add in some more error checking, help save your sanity later.

Go back and fix your update function so that it works if env is { }. Remember that JavaScript is a call-by-value language, so you can't just do env = ... inside your function. Luckily you can use env.key = ... to modify the value of env.

Function application

Start on general function application by first supporting function values with exactly one argument. Once that is working and you've written a few tests, expand function application to cover any number of arguments including no arguments. Test it out by adding some pre-defined JavaScript functions in the initial environment that take different numbers of arguments.

In your implementation you might want to call JavaScript functions with different numbers of arguments in an array. Look into how apply works. Here is a quick example:

lambda

Next add in lambda-one. Make sure it works with existing features. Add some tests to make sure it's behaving as you expect.

Then work on lambda which takes a list of arguments rather than just one. When you bind the arguments in a new environment, the environments we've been using support binding multiple variables at once.

Add unit tests that cover simple and complicated examples of function calls. This is one of those times where the testing and validation can be more complicated than the implementation itself.

Here are some ideas of things to check in your implementation. You don't have to do all of these, but do at least a few interesting cases.

  1. Defining a function
  2. Simple function calls
  3. Calling an anonymous function
  4. Passing a function as a value to another function
  5. Inner function uses values from enclosing function
  6. Argument to a function shadows a global variable
  7. A function modifies a global variable
  8. An inner function modifies a variable in the outer function
  9. An outer function returns an inner function
  10. An outer function returns an inner function, inner function refers to outer function variables
  11. A function in a define that calls itself recursively


Prev Next