University of Virginia, Department of Computer Science
CS200: Computer Science, Spring 2003

Exam 2 - Comments

Sticker Interpretation

If you have a sticker on your returned exam, it can be interpreted as a lower bound (Ω) on your grade in the course (as long as you do a satisfactory job completing PS8) as described below. If there is no sticker on your exam, it means you definitely need to do the final.

Mutation and Objects

1. The following environment diagram shows the state after evaluating (define discovery (make-sail-boat "discovery")). The procedure associated with the name discovery is missing its environment pointer. Add the missing environment pointer to the diagram.
Answer: The environment pointer for the discovery procedure should point to F4. The discorvery procedure results from the application (make-sail-boat "discovery"). Applications create frames. In the body of make-sail-boat the (let ((super (make-boat name) (sailing #f))) is syntactic sugar for another application. It creates the frame F4 (that has places for super and sailing.
2. There are three frames in the diagram (labeled F1, F2 and F3) that contain a place named name with the value "discovery". If we evaluate (ask discovery 'name) in the global environment, in which frame with the value of name be found?
Answer: F1.

Evaluating the discovery procedure leads to the call to (get-method super message) since there is no name method in sail-boat. We look for super following the procedure's environment pointer, and find it in F4. The value of super in F4 is the result of (make-boat name). Evaluating it for name will evaluate (get-method super message) since boat does not have a name method. We look up super following the environment pointer, and find it in F5. Here, it is the result of (make-object name). We find the name method, and lookup name in the procedure's environment, which is F1.

Metalinguistics

3. Explain why I. M. will not be able to define repeatuntil as a procedure herself in Mini-Scheme, but must instead change the evaluator.
Answer: repeatuntil must be a special form since its operands are not evaluated using the normal application evaluation rule. For example, the second subexpression should only be evaluated if the first subexpression evaluates to false.
4. I. M. has asked you to extend Mini-Scheme to support repeatuntil. Provide the missing definition for eval-repeatuntil. The new code is shown below; you may assume everything else from PS7's meval is available.
Answer:
(define (eval-repeatuntil expr env)
  (if (meval (repeatuntil-test expr) env)
      (meval (repeatuntil-result expr) env)
      (begin
	(meval (repeatuntil-body expr) env)
	(meval expr env)))) ;; keep repeating

Problems about Problems

5. Minimum Value Problem. Find the smallest value in a list.
Input: A non-empty list of n numbers.
Output: The smallest (using < to compare numbers) number in the input list.
Answer: Decidable. We can solve this with a procedure that cdr's down the list recursively, keeping track of the smallest value it has found so far. This will take O(n) work since doubling the size of the list would double the amount of work it requires. Since there is no way to solve this problem without at least looking at every element in the list, we know it is Ω(n). Since we have the same upper and lower bounds, we also know this problem is Θ(n).
6. Greater than Zero Problem.
Input: A program P and input n
Output: If evaluating P applied to n would produce a value greater than 0, true. If evaluating P applied to n would produce a value that is not a number, or is a number less than or equal to 0, or would not terminate, output is false.
Answer: Undecidable. If we could solve the Greater than Zero problem, we could also solve the halting problem, but we know that the halting problemis undecidable. Here's how:
(define (halts? P n)
  (greater-than-zero? (lambda (x) (P n) 1)))
If (P n) would halt, then we know (lambda (x) (P n) 1) will evaluate to 1 and greater-than-zero? will be true. If (P n) doesn't halt, then (lambda (x) (P n) 1) also doesn't halt, and greater-than-zero? will be false. Hence, we know greater-than-zero? must be undecidable, since we could use it to define halts?.

7. Many Goldstars Problem. The input tape is n star symbols followed by the end of tape symbol. The output tape is 2n star symbols followed by the end of tape symbol.

Input: n stars
Output: 2n stars
Answer: Decidable. It will require at least Ω(2n) work since the output size is 2n, and a Turing machine can write at most one square each step. It is also possible to solve it with O(2n) work since calculating 2n can be done quickly (certainly in O(n2) since it involves n multiplications, and each multiplication might be at worst O(n) work), and then we just need to write that output onto the tape, keeping track of our work by crossing off symbols. So, it is Θ(2n). Note that this problem is not in the NP complexity class — it requires more than polynomial work to verify the answer, since the output is so big.

Lambda Calculus

8. Show how T and F would need to be defined to make the new definition of if work correctly.

Answer: We need to swap the meaning of T and F to work with our new if:
T ≡ λ xy . y
F ≡ λ xy . x

Universal Language

9. Is PreSQL a Universal Language? Use convincing arguments to support your answer.
Answer: No, it is not a Universal Language since there are computations a Turing machine can do that cannot be simulated by PreSQL. We know a Turing machine can perform a computation that never halts. But, every computation in PreSQL terminates. There is no way to loop or jump, and every PreSQL instruction terminates, so the time a computation takes is determined by the number of instructions in the input, which must be finite.
10. Show how you could do addition using PreSQLsql. Your addition code should assume the input is in the form of two tables, tableA and tableB. After addition, a new table named output should have the result. Provide code that could be used for the missing code below that would correctly add any two natural numbers (your code may modify the input tables):
Answer:

> CREATE TABLE output

> UNTILEMPTY tableA { DELETE FROM tableA; INSERT INTO output TRUE}

> UNTILEMPTY tableB { DELETE FROM tableB; INSERT INTO output TRUE}

CS 200


CS 200: Computer Science
Department of Computer Science
University of Virginia

Circle Fractal by Ramsey Arnaoot and Qi Wang

cs200-staff@cs.virginia.edu
Using these Materials