Docsity

Solutions to Midterm 1 CS 421 Spring 2007: OCaml Programming, Exams of Computer Science

Toggle sidebar
bg1
Solutions for Sample Questions for Midterm 1 (CS 421 Spring 2007)
On the actual midterm, you will have plenty of space to put your answers.
Some of these questions may be reused for the exam.
1. Given the following OCAML code:
let x = 3;;
let f y = x + y;;
let x = 5;;
let z = f 2;;
let x = โ€œhiโ€;;
What value will z have? Will the last declaration (let x = โ€œhiโ€;) cause a type error?
What is the value of x after this code has been executed?
Solution:
z is bound to 5
let x = โ€œhiโ€ will not cause a type error
x is bound to โ€œhiโ€
2. What environment is in effect after each declaration in the code in Problem 1?
Solution:
let x = 3;;
{x โ†’ 3}
let f y = x + y;;
{f โ†’ <y โ†’ x+y, {x โ†’ 3}>, x โ†’ 3}
let x = 5;;
{x โ†’ 5} + {f โ†’ <y โ†’ x+y, {x โ†’ 3}>, x โ†’ 3} =
{x โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>}
let z = f 2;;
{z โ†’ 5, x โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>}
let x = โ€œhiโ€;;
{x โ†’ โ€œhiโ€} + {z โ†’ 5, x โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>} =
{x โ†’ โ€œhiโ€, z โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>}
3. 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).
a. For each program, what behavior would you expect to see?
b. What is the difference between loop1 and loop2?
c. For each program state if it is:
i. recursive,
ii. forward recursive,
pf3

Partial preview of the text

Download Solutions to Midterm 1 CS 421 Spring 2007: OCaml Programming and more Exams Computer Science in PDF only on Docsity!

Solutions for Sample Questions for Midterm 1 (CS 421 Spring 2007)

On the actual midterm, you will have plenty of space to put your answers.

Some of these questions may be reused for the exam.

  1. Given the following OCAML code: let x = 3;; let f y = x + y;; let x = 5;; let z = f 2;; let x = โ€œhiโ€;; What value will z have? Will the last declaration ( let x = โ€œhiโ€; ) cause a type error? What is the value of x after this code has been executed? Solution: z is bound to 5 let x = โ€œhiโ€ will not cause a type error x is bound to โ€œhiโ€
  2. What environment is in effect after each declaration in the code in Problem 1? Solution: let x = 3;; {x โ†’ 3} let f y = x + y;; {f โ†’ <y โ†’ x+y, {x โ†’ 3}>, x โ†’ 3} let x = 5;; {x โ†’ 5} + {f โ†’ <y โ†’ x+y, {x โ†’ 3}>, x โ†’ 3} = {x โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>} let z = f 2;; {z โ†’ 5, x โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>} let x = โ€œhiโ€;; {x โ†’ โ€œhiโ€} + {z โ†’ 5, x โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>} = {x โ†’ โ€œhiโ€, z โ†’ 5, f โ†’ <y โ†’ x+y, {x โ†’ 3}>}
  3. 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). a. For each program, what behavior would you expect to see? b. What is the difference between loop1 and loop2? c. For each program state if it is: i. recursive, ii. forward recursive,

iii. tail-recursive. Solution: a. The first program generates a stack overflow, while the second program runs indefinitely. b. Both programs are recursive, and in fact forward recursive, but loop2 is tail- recursive while loop1 is not. c. Because loop1 is not tail-recursive, each new recursive call must push a new activation record onto the stack, hence the stack overflow, but since loop2 is tail- recursive, each new activation record may overwrite the previous call, and thus the stack does not grow. It should also be observed that loop2 has a more general type ( :unit -> โ€™a ) than that of loop1:unit -> unit , and hence may be used in places where other return types beside unit are required. (Of course, it had never actually be applied.)

  1. Write an OCAML function pair_up that takes first a function, then an input list and returns a list of pairs of an element from input list (the second argument), paired with the result of applying the first argument to that element. What is the OCAML type of pair_up? What is the result of the following expressions: a. pair_up (fun x -> x + 3) [6;4;1];; b. pair_up ((fun x -> โ€œHi, โ€œ^x), [โ€œJohnโ€; โ€œMaryโ€;โ€œDanaโ€]);; c. *pair_up (fun x -> x . 2.0);; Solution: let rec pair_up f l = (match l with [] -> [] | x :: xs -> (x, f x)::pair_up f xs) alternately let pair_up f = map (fun x -> (x, f x)) pair_up : (โ€˜a -> โ€˜b) -> โ€˜a list -> (โ€˜a * โ€˜b) list a. [(6, 9); (4,7); (1,4)];; b. type error c. A function of type float list -> (float * float) list that returns a list of pairs of an element from the input list paired with twice itself.
  2. Write an Ocaml function palindrome :string list -> unit that first prints the strings in the list from left to right, followed by printing them right to left, recursing over the list only once. (Potential extra credit problem: Do this using each of List.fold_right and List.fold_left but no explicit use of let rec .) Solution: let rec palindrome l = match l with [] -> () | s::ss -> (print_string s; palindrome ss; print_string s);; let rec palindrome l = List.fold_right (fun s -> fun print_middle -> (fun () -> (print_string s; print_middle (); print_string s)))