Now that our Curly interpreter supports functions, we should be able to interpret recursive Curly programs. For example, we'd like to be able to evaluate a program that sums the numbers from a given number down to zero. In order to do this, though, we need to augment Curly and the interpreter to handle:
For definitions, we only want top-level defines. Defines may not occur inside other expressions.
For conditionals, we will add a simple version of if called if-zero. The test-expression in an if-zero statement should evaluate to a number. If the expression returns zero, the then branch is taken, otherwise the else branch is taken.
Given define and if-zero, the following would now be a valid Curly program:
{define sum-down
{proc {anum}
{ if-zero anum
then return{0}
else return{ anum + sum-down{ anum + -1 }}}}
}
Augment the data definition for Curly to include definitions and if-zero expressions. Think carefully about whether each piece extends our expr definition or becomes a separate data definition. If something extends expr, it can be used anywhere an expr is valid.
Supporting definitions requires a small change in the contract for the interpreter. Now, it must take the expression to evaluate and the list of definitions that have been entered into the top window of DrCurly:
interp : expr list[definition] -> value
Modify the interpreter to handle if-zero expressions and definitions.
Write the expression for sum-down in your data model and test calls to sum-down with your extended interpreter.
Turn in a single file hwk4.ss (or hwk4.scm) containing all code and documentation for this assignment. Make sure that both students' names are in a comment at the top of the file.
Refer to the Laws of Homework when preparing your solutions.