



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
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;; ( 5 *)
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:
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));;
Solution: Printed on the screen is:
val f : (unit -> ’a) -> int -> int =
abcdfgj val u : int = 8
First, the evaluation of the declaration of the f generates no observable behavior, beside the final printing of its type information, because it immediately evaluates to a closure (〈g 7 → fun x -> (let r =... ), ρinit〉 where ρinit is the environment with which we start. The evaluation of u proceeds as follows:
(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];;
Indicate what observable behavior (as described in the previous problem) will occur with the evaluation of each declaration. Explain why the evaluation of each declaration generates the described behavior. You may omit explanations of type information.
Solution: In evaluating the declaration of double1, first printing Calling double1 ..., and then binding double1 to the closure corresponding to (fun () -> fun x -> x (^) * 2), the second expression in the sequence in the body of double1. The other two procedures, double2, and double3 evaluate to closures immediately.
Calling double ... val double1 : unit -> int -> int = val double2 : unit -> int -> int = val double3 : unit -> int -> int =
Evaluating double1 does no printing because there are no calls to print string in its closure (corresponding to (fun () -> fun x -> x * 2)). Therefore, when evaluating the declarations for result1, result4, and result7, mapping double1 () over the list [1; 2; 3], just the resultant list is returned. Thus the observable behavior for result1, result4, and result7 is just the following:
val result1 : int list = [2; 4; 6]
val result4 : int list = [2; 4; 6]
val result7 : int list = [2; 4; 6]
The application of the procedure double2 to its first argument, texttt(), causes it to print "Calling double ... \n" and then to return the closure corresponding to (fun x -> x (^) * 2). In the evaluation of result2, double2 () is evaluated once in the let...in clause and the resulting function is mapped over [1;2;3], the phrase "Calling double2 ...\n" will only be printed once. Thus we have
Calling double ... val result2 : int list = [2; 4; 6]
In the evaluation of result5, the argument (double2 ()) must be evaluated before the application List.map (double1 ()) may be evaluated. The evaluation of (double2 ()) happens only this one time and so, again, the phrase "Calling double2 ...\n" will only be printed once.
Calling double ... val result5 : int list = [2; 4; 6]
The situation changes when we evaluate result8. Here, by lambda lifting the application of result2, List.map is not applied to the result of the application of double2 to (), but rather to the closure corre- sponding to (fun x -> double2 () x). We only execute the print statement after we haev applied this to an integer, which happens once per element in the list. Thus we get:
Calling double ... Calling double ... Calling double ... val result8 : int list = [2; 4; 6]
In the case of double3, the print statement is in the body of the innermost funciton, and hence will not be executed until it has been applied to both its arguments. Thus it will print "Calling double3 ... \n" exactly when it is applied to both () and to an integer, This happens exaclty once for each of the elements in the list [1;2;3] over which double3 () is being mapped. Thus we have:
Calling double ... Calling double ... Calling double ... val result3 : int list = [2; 4; 6]
Calling double ... Calling double ... Calling double ... val result6 : int list = [2; 4; 6]
Calling double ... Calling double ... Calling double ... val result9 : int list = [2; 4; 6]
let rec weird n = if n <= 0 then 0 else if weird(n - 1) mod 2 = 0 then weird (n - 1) / 2 else weird (n - 1) - 1;;
Solution: The original code was (O(2n) because every step required two recursive calls on input size n. Since these calls were in fact the exact same computations, we can convert this code to linear (O(n)) by moving the common subexpression outside the inner if with a local declaration.
let rec weird n = if n <= 0 then 0 else let r = weird (n - 1) in if r mod 2 = 0 then r / 2 else r - 1;;