Docsity

CS 421 HW 1: Understanding Evaluation and Environments in OCaml - Prof. Elsa Gunter, Assignments of Computer Science

Toggle sidebar
bg1
HW 1 – Evaluation and Environments
CS 421 – Fall 2006
Revision 1.0
Assigned September 12, 2006
Due September 20, 2006, 9:00 AM - Note the unusual time!
To be turned in at 2106 Siebel Center, office of Andrea Whitesell
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 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.
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 midterm and final.
4 Problems
1. Consider the following two OCaml functions, loop1 and loop2:
let rec loop1 () = loop1(); ()
let rec loop2 () = loop2();;
val loop1 : unit -> unit = <fun>
val loop2 : unit -> ’a = <fun>
Suppose you were to run loop1();; and loop2();; in OCaml, (pressing CTRL + C after at least a minute to
terminate infinite loops when necessary).
1. For each program, what behavior would you expect to see?
2. What is the difference between loop1 and loop2?
3. For each program state if it is:
1
pf2

Partial preview of the text

Download CS 421 HW 1: Understanding Evaluation and Environments in OCaml - Prof. Elsa Gunter and more Assignments Computer Science in PDF only on Docsity!

HW 1 – Evaluation and Environments

CS 421 – Fall 2006

Revision 1.

Assigned September 12, 2006

Due September 20, 2006, 9:00 AM - Note the unusual time!

To be turned in at 2106 Siebel Center, office of Andrea Whitesell

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 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.

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 midterm and final.

4 Problems

  1. Consider the following two OCaml functions, loop1 and loop2:

let rec loop1 () = loop1(); () let rec loop2 () = loop2();; val loop1 : unit -> unit = val loop2 : unit -> ’a =

Suppose you were to run loop1();; and loop2();; in OCaml, (pressing CTRL + C after at least a minute to terminate infinite loops when necessary).

  1. For each program, what behavior would you expect to see?
  2. What is the difference between loop1 and loop2?
  3. For each program state if it is:

(a) recursive, (b) forward recursive, (c) tail-recursive.

  1. Consider the following body of code:

(* 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.

  1. Below is a fragment of OCaml code, with various program points indicated by numbers with comments.

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.

  1. (Extra Credit) Describe the environment in effect at program point 5.