

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.0 Initial Release.
Your answers to the following questions are to be hand-written neatly on one or more sheets of paper, each with your name in the upper right corner. The homework is to be turned in to my administrative assistant, Andrea Whitesell in 2106 Siebel Center by 9:00am of Wednesday 20 September 2006. 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 midterm and final.
let rec loop1 () = loop1(); () let rec loop2 () = loop2();; val loop1 : unit -> unit =
Suppose you were to run loop1();; and loop2();; in OCaml, (pressing CTRL + C after at least a minute to terminate infinite loops when necessary).
(a) recursive, (b) forward recursive, (c) tail-recursive.
(* 1 ) let square1 = fun () -> fun x -> (print_string "Calling square1 \n"; x (^) * x);; ( 2 ) let square2 = fun () -> (print_string "Calling square2 ...n"; (fun x -> x (^) * x));; ( 3 ) let square3 = (print_string "Calling square3 ...n"; (fun () -> fun x -> x (^) * x));; ( 4 ) let result1 = let s = square1 () in List.map s [1;2;3];; ( 5 ) let result2 = let s = square2 () in List.map s [1;2;3];; ( 6 ) let result3 = let s = square3 () in List.map s [1;2;3];; ( 7 ) let result4 = List.map (square1 ()) [1;2;3];; ( 8 ) let result5 = List.map (square2 ()) [1;2;3];; ( 9 ) let result6 = List.map (square3 ()) [1;2;3];; ( 10 (^) ) let result7 = List.map (fun x -> square1 () x) [1;2;3];; ( 11 (^) ) let result8 = List.map (fun x -> square2 () x) [1;2;3];; ( 12 (^) *) let result9 = List.map (fun x -> square3 () x) [1;2;3];;
Indicate what observable behavior (i.e. everything you see displayed on the screen) will occur with the evaluation of each declaration. Explain why the evaluation of each declaration generates the described behavior.
let x = 5;; (* 1 (^) ) let y = 17;; ( 2 (^) ) let w = let x = 22 in x + 2;; ( 3 (^) ) let f x = x + y;; ( 4 (^) ) let g = let f = fun x -> x + 3 in let z = 21 in f (x + z);; ( 5 *)
For each of program points 1, 2, 3 and 4, 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 cummulative thereafter. The program points are supposed to indicates points at which the immediately preceding top-level declaration has been fully evaluated.