# hw1-sol - HW 1 Evaluation and Environments CS 421 Fall 2007...

This preview shows pages 1–3. Sign up to view the full content.

HW 1 – Evaluation and Environments CS 421 – Fall 2007 Revision 1.0 Assigned September 4, 2007 Due September 11, 2007, 2:00pm, in class Extension 48 hours (20% penalty) 1 Change Log 1.0 Initial Release. 2 Turn-In Procedure Your answers to the following questions are to be hand-written, or printed, neatly on one or more sheets of paper, each with your name in the upper right corner. The homework is to be turned in in class at the start of class. Alternately, you may hand it to Prof. Elsa Gunter in person before the deadline. 3 Objectives and Background The purpose of this HW is to test your understanding of the order of evaluation of expressions in OCaml; the scope of variables, and the state of environments used during evaluation Another purpose of HWs is to provide you with experience answering non-programming written questions of the kind you may experience on the midterms and final. 4 Problems 1. (15 pts) Below is a fragment of OCaml code, with various program points indicated by numbers with comments. let x = 3.9;; let y = 17;; ( * 1 * ) let z = let x = 4 in ( * 2 * ) x + y;; ( * 3 * ) let f x y = x + y + z;; ( * 4 * ) let g w = f w w;; let z = g z;; ( * 5 * ) 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
For each of program points 1, 2, 3, 4 and 5, please describe the environment in effect after evaluation has reached that point. You may assume that the evaluation begins in an empty environment, and that the environment is cumu- lative thereafter. The program points are supposed to indicate points at which all complete preceding declarations have been fully evaluated. In describing the environments 1 through 4, you may use set notation, as done in class, or you may use the update operator + . If you use set notation, no duplicate bindings should occur. The answer for program point 5 should be written out fully in set notation. Solution: ρ 1 = { y 17 , x 3 . 9 } ρ 2 = { x 4 } + ρ 1 = { x 4 , y 17 } ρ 3 = { z 21 } + ρ 1 = { z 21 , y 17 , x 3 . 9 } ρ 4 = { f x fun y -> x + y + z , ρ 3 } + ρ 3 = { f x fun y -> x + y + z , ρ 3 , z 21 , y 17 , x 3 } { z 63 , g w f w w , ρ 4 , f x fun y -> x + y + z , ρ 3 , y 17 , x 3 } = { z 63 , g w f w w , { f x fun y -> x + y + z , { z 21 , y 17 , x 3 . 9 } , z 21 , y 17 , x 3 } , f x fun y -> x + y + z , { z 21 , y 17 , x 3 . 9 } , y 17 , x 3 } 2. (20 pts) Below is a fragment of Ocaml code. Describe everything that is displayed on the screen (its observable
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern