Com S 342 --- Principles of Programming Languages EXERCISE 12: LAZY EVALUATION (File $Date: 2005/04/13 06:21:38 $) The purpose of this exercise is for you to learn about lazy evaluation. As with all exercises, this is to be done individually. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs342/syllabus.shtml). We expect this to help you think about the readings (see below). If you don't have an answer or an answer that you think is good, write down what you read, and a question or two that would have helped you make progress. Then you can ask that question in class; there will be other people with the same problem, and everyone can learn by discussing these issues. And you'll most likely see similar things on the homework, so it's best to understand them now. READINGS: Read section 3.8 of "Essentials of Programming Languages" (2nd ed., 2001) by Friedman, Wand, and Haynes, starting at page 115. Read the code carefully. 1. [Lazy evaluation] a. Are parameters to functions in C++ eagerly or lazily evaluated? b. Are parameters to procedures in Scheme eagerly or lazily evaluated? c. In the call-by-need interpreter of section 3.8 (load "3-8need.scm"), what is the result of the following program: letrec forevermore(n) = (forevermore +(n, 1)) in let myif = proc(test, trueval, falseval) if test then trueval else falseval in (myif 1 342 (forevermore 0)) d. What would happen if you ran the above in the interpreter of section 3.7? e. Why is lazy evaluation necessary for allowing users to write their own control structures as procedures? 2. [call by name vs. call by need] Consider the following defined language code: letrec mywhile(test, body) = if test then begin body; (mywhile test body) end else 0 in let x = 10 in begin (mywhile x set x = sub1(x)); x end a. What is the value returned, if any, by the above program using call by name? b. What is the value returned, if any, by the above program using call by need? WHAT TO HAND IN You should have at the beginning of class, your answers to the above questions (or questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above.