Assignment 11, due Nov 17

Part of the homework for CS:2820, Fall 2017
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Assignments are to be turned in on paper. On every assignment, write your name, course and section number at the top. Write your name as it appears in your university records! Use the section number for which you are registered! We will not do detective work to figure out who did what. Work must be legible. Homework is due on paper in discussion section, usually at the start except when the assignment indicates that time to work on the assignment will be provided in class. Exceptions will be made only by advance arrangement with your TA (excepting "acts of God" outside your control). Never push homework under someone's door!

  1. Background: In Friday's discussion section, code something like this was presented as yet another example of lambda expressions:
    public class Lazy {
        public static interface Int {
            int eval();
        }
        public static Int add( Int i, Int j ) { return ()->i.eval() + j.eval(); }
        public static Int term( int i ) { return ()->i; }
    
        public static void main(String[] args) {          // 1
            Int i = term( 1 );                            // 2
            Int j = term( 0 );                            // 3
            for (int x = 0; x < 5; x++) {                 // 4
                Int k = j;                                // 5
                j = i;                                    // 6
                i = add( j, k );                          // 7
                System.out.println( "gets " + k.eval() ); // 8
            }                                             // 9
        }                                                 //10
    }
    

    This code has been tested. It works. It is obvious that the add method is called exactly 5 times by the above loop, once for each execution of line 7 in the code.

    a) The integer add operation inside the lambda expression in the add method is performed a different number of times by this code. How many? (Hint: You can get the answer from understanding Java and lambda expressions, or you can rewrite the code to count the additions.) (0.5 points)

    b) Which line of the above code actually causes the integer additions discussed in part a) to be performed. (0.5 points)

    c) Look up lazy evaluation in Wikipedia. The code given above emboies one of the key properties of lazy evaluation defined there, but not the other. Which property has it missed (what you learned in the first parts of this question are relevant).

  2. Background: Consider the new simulation framework given in Lecture 24 and the old λ-expression-based framework given in Lecture 14 and used with machine problems 4 and 5.

    (Hint: In this question, focus on the nature of the activity being scheduled, that is, the body of the Lambda expression or trigger method. Also note that none of the code in the simulations we've written creates one of the counterexamples that are the subject here.)

    a) Can any call to Simulation.schedule written in the old framework be rewritten as a single call to Simulation.schedule in the new one? If not, provide a counterexample. (0.5 points)

    b) Can any call to Simulation.schedule written in the new framework be rewritten as a single call to Simulation.schedule in the old one? If not, provide a counterexample. (0.5 points)

    c) Rewrite any counterexamples you came up with so that they work under the framework where they couldn't be written as a single call to Simulation.schedule under the other framework. (Big hint, two calls might suffice, or perhaps new method is needed.) (0.5 points)