Com S 342 --- Principles of Programming Languages EXERCISE 13: DYNAMIC SCOPING (File $Date: 2004/04/06 06:10:44 $) The purpose of this exercise is for you to learn about dynamic binding (also known as dynamic scoping). 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). As with all exercises, you have two choices for doing the work. You can either: - complete it as specified or - write down questions or problems that you had in trying to complete it. If you write down questions or problems you have, these should be detailed enough so that we can tell that you have read the materials and thought about them. (Don't just write: "I didn't understand how to do it". Instead, say what you tried and what you didn't understand.) During the class where this exercise is discussed, you should ask about these difficulties, and clear them up. Don't be shy; 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. 1. [Understanding dynamic binding/scoping] Read exercises 3.30-3.33 on pages 91-92 of "Essentials of Programming Languages" (2nd ed., 2001) by Friedman, Wand, and Haynes. Consider the following expression in the defined language. let y = 342; z = 541 in let f = proc(x) list(y, +(x, z)); %%% draw the picture when execution is here p = proc(y) f(let z = 52 in +(z, 3)) in let y = 5; z = 10 in p(+(y, z)) Using dynamic scoping, (a) draw a picture of the run-time stack when execution reaches the point indicated (with the stack growing up the page), and (b) give the result (if any) of the above expression. (If the expression has no result, or encounters an error, write that.) 2. [Understanding dynamic binding/scoping] Consider the following expression in the defined langauge let x = 3; y = 5 in let p = proc(i, k) %% draw the picture when execution is here list(x, y, i, k) x = 7 in let x = 9 in let ls = p(let y = 11 in y, x) in cons(x, cons(y, ls)) Using dynamic scoping, (a) draw a picture of the run-time stack when execution reaches the point indicated (with the stack growing down the page), and (b) give the result (if any) of the above expression. If the expression has no result, or encounters an error, write that and briefly explain what the problem is. WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above.