Homework for Oct 5 In this homework, you will implement a small extension to the language defined in interp-struct.scm. This exercise doesn't require you to write a lot of code, but it does require you to completely understand the program covered in class on Thursday (Sep 28). It wouldn't be a bad idea to review that program now before proceeding further and make sure you're comfortable with it. The language as we have defined it so far models certain aspects of logical circuits. You can define input wires, associated with variables, and you can supply those inputs to any number of gates. You can wire the gates together so that you use the output of any gate as the input to another gate. However, you cannot yet split the output of a gate so that it can serve as the input to two other gates. Check this for yourself - the best you can do now is to copy the whole circuit so that the same logical function gets computed each time it is used. We will fix this in this assignment. We will create an extension to the language to describe a circuit in which the output of a complex circuit can be reused throughout the rest of the circuit. Here is the metaphor behind this extension. We think of the output of the circuit that we want to copy as defining its own variable. The circuit defines the value of this variable. Then there is another logical expression that uses this value as often as is needed, by referencing the variable we have created. Here is a statement that can introduce this new construction. ; The line below defines: make-split split? split-num split-val split-expr (define-struct split (num val expr) (make-inspector)) I have called this "split" because it lets us split the wire coming out of a gate and use it many times. The three arguments are num, for the variable that is created, val for the value that the variable should have that's computed by the split circuit, and expr for the remainder of the circuit that reuses the copied value. Concretely, this statement now defines a circuit that implements the exclusive-or function: ; an implementation of exclusive-or ; using split (define testex (make-split 2 (make-negate (make-binary (make-variable 0) 'and (make-variable 1))) (make-binary (make-binary (make-variable 0) 'and (make-variable 2)) 'or (make-binary (make-variable 1) 'and (make-variable 2))))) First we check that lines 0 and 1 are not both true, and store this on line 2. That way, when we check that either 0 and 2 are true or 1 and 2 are true, it has the effect of saying that either 0 and 1 are true, but not both. Your job in this homework is to add the conditional case to the interp function to handle the split construction. It's worth stopping now and thinking for yourself about what this code is going to have to do. The split will be represented by a structure a for which (split? a) is true. The questions you have to answer are: How do you compute the result of the circuit associated with (split-val a)? What do you do with the result? And how do you compute the result of the circuit associated with (split-expr a)? If you've thought about the problem, you should be able to write down an English statement at this point that describes the overall computation that you have to implement. Many of you will find it helpful to write this statement down before you start attacking the problem in Scheme. We've given you a helper function that you can use to carry out the final implementation. It is called subst, and you can use it to set the value of a specific variable in the environment. ; create a list just like l ; except to replace the nth element of l with e (define (subst l n e) (let ((this (if (null? l) 'L (car l))) (next (if (null? l) '() (cdr l)))) (if (= 0 n) (cons e next) (cons this (subst next (- n 1) e))))) It's a little fancy because it creates the position in the list that you're setting if it doesn't exist yet. That way when you do something like (interp testex '(L H)) you can pass an environment that only defines the input variables 0 and 1 and the split variable 2 will then be created as it is needed. This is a bit of trickiness that is independent of the thinking and programming we want you to do for this assignment, which is why we're supplying this code for you.