Homework #19


This problem requires Lecture 17 and book section 3.3.

This exercise is not from the book. The interpreter does not have logical operators. This exercise will help you explore adding them as regular primitives and as special forms.

Part A - variable arity 'or' - (30 minutes)
Add a variable arity primitive called "or" to the interpreter.  The function should return the boolean disjunction of its arguments (i.e. if any of the values are not zero, it should return 1).

Part B - short-cicuited 'sum' - (30 minutes)
Add a variable arity special form "sum" function to the interpreter that returns the boolean disjunction of its arguments, but performs "short-circuited evaluation" as we discussed in Lecture 5.  This means that "sum" should only evaluate expressions until it finds one that is true.

The parser should return a sum-exp node in the abstract syntax tree when given the string "sum(1,0,1,1)".  The abstract syntax for a sum-exp node is sum-exp(rands).  The process for adding this special form is just like the process in Lecture 17.

Note: this special form is called "sum" to distinguish it from the primitive function "or".  In logic, "or" is sometimes called the "disjunctive sum".

Part C - follow-up question - (10 minutes)
Why couldn't such short-circuited logic have been built into the primitive procedures?

Here is the code for the Lecture 17 interpreter to start with.

Here's a quick test you can use:
Suppose that the variable z is undefined. The expression or(1,z) should crash. The expression sum(1,z) should return 1.

Note: be very careful to keep straight ELL's logical values (zero and non-zero) and Scheme's logical values (#f and #t). Only ELL logical values should ever be visible to a programmer using the interpreter.


You must log in first before submitting homework assignments.

Instructions on submitting homework is here


Last updated at 9:53 am on Friday, August 27, 2004.