

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


1.1 Changed strange to weird in the last problem.
1.0 Initial Release.
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.
The purpose of this HW is to test your understanding of
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.
let x = "a";; let y = 8;; (* 1 ) let f x z = x + y + z;; ( 2 ) let z = let y = "b" in ( 3 ) x ˆ y;; ( 4 *) let g w = f w w;; let y = g y;;
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.
let f g x = (let r = if (((print_string "a"; x > 2) && (print_string "b"; x > 10)) && (let y = (print_string "c"; 14) in (print_string "d"; x > y) || (print_string "e"; x < 5))) then let z = (print_string "f"; 7) in (print_string "g"; x - z) else let z = (print_string "h"; 15) in (print_string "i"; z) in (g(); r));; let u = (f (fun () -> print_string "j\n") (f (fun () -> print_string "k\n") 3));;
(* 1 ) let double1 = (print_string "Calling double1 ... \n"; (fun () -> fun x -> x (^) * 2));; ( 2 ) let double2 = fun () -> (print_string "Calling double2 ... \n"; (fun x -> x (^) * 2));; ( 3 ) let double3 = fun () -> fun x -> (print_string "Calling double3 ... \n"; x (^) * 2);; ( 4 ) let result1 = let s = double1 () in List.map s [1;2;3];; ( 5 ) let result2 = let s = double2 () in List.map s [1;2;3];; ( 6 ) let result3 = let s = double3 () in List.map s [1;2;3];; ( 7 ) let result4 = List.map (double1 ()) [1;2;3];; ( 8 ) let result5 = List.map (double2 ()) [1;2;3];; ( 9 ) let result6 = List.map (double3 ()) [1;2;3];; ( 10 (^) ) let result7 = List.map (fun x -> double1 () x) [1;2;3];; ( 11 (^) ) let result8 = List.map (fun x -> double2 () x) [1;2;3];; ( 12 (^) *) let result9 = List.map (fun x -> double3 () x) [1;2;3];;