Solutions Sample Questions for Midterm - Programming Languages and Compilers | CS 421, Exams of Computer Science

Material Type: Exam; Class: Progrmg Languages & Compilers; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2006;

Typology: Exams

Pre 2010

Uploaded on 03/16/2009

koofers-user-oiy
koofers-user-oiy 🇺🇸

5

(1)

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solutions for Sample Questions for Midterm (CS 421 Fall 2006)
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. Given the following OCAML datatype:
type int_seq = Null | Snoc of (int_seq * int)
write a tail-recursive function in OCAML all_pos : int_seq -> bool that returns true if every
integer in the input int_seq to which all_pos is applied is strictly greater than 0 and false
otherwise. Thus all_pos (Snoc(Snoc(Snoc(Null,3),5),7)) should returns true, but
all_pos (Snoc(Null,~1)) and all_pos (Snoc(Snoc(Null, 3),0)) should both return false.
Solution:
let rec all_pos s =
(match s with Null -> true
| Snoc(seq, x) -> if x <= 0 then false else all_pos seq);;
pf3
pf4
pf5

Partial preview of the text

Download Solutions Sample Questions for Midterm - Programming Languages and Compilers | CS 421 and more Exams Computer Science in PDF only on Docsity!

Solutions for Sample Questions for Midterm (CS 421 Fall 2006)

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. Given the following OCAML datatype: type int_seq = Null | Snoc of (int_seq * int) write a tail-recursive function in OCAML all_pos : int_seq -> bool that returns true if every integer in the input int_seq to which all_pos is applied is strictly greater than 0 and false otherwise. Thus all_pos (Snoc(Snoc(Snoc(Null,3),5),7)) should returns true, but all_pos (Snoc(Null,~1)) and all_pos (Snoc(Snoc(Null, 3),0)) should both return false. Solution: let rec all_pos s = (match s with Null -> true | Snoc(seq, x) -> if x <= 0 then false else all_pos seq);;
  1. Write an OCAML function pair_up 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 which returns a list of pairs of an lement from the input list pard 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))) l (fun () -> ()) (); let palindrome l = List.fold_left (fun print_middle -> fun s -> (print_string s; fun () -> (print_middle (print_string s)))) (fun () -> ()) l ();;
  3. Using the rules provided in class, derive a valid type judgment for let rec fact = fun n -> if n = 0 then 1 else let r = fact (n - 1) in n * r in fact;; (The rules will be provided for you on the exam, if this kind of question is asked.) Solution: (I didn’t just write the tree because it wouldn’t fit.)

(15) and (16) are both valid by the variable rule. Hence (10) is valid. Hence (6) is valid. Hence (3) is valid, and hence (1) is valid

  1. Write the definition of an OCAML variant type reg_exp to express abstract syntax trees for regular expressions over a base character set of booleans. Thus, a boolean is a reg_exp, epsilon is a reg_exp ,the concatentation of two reg_exp’s is a reg_exp, the “choice” of two reg_exp’s is a reg_exp, and the Kleene star of a reg_exp is a reg_exp. Solution: type reg_exp = Epsilon | Var of bool | Choice of (reg_exp * reg_exp) | Concat of (reg_exp * reg_exp) | Kleene_star of reg_exp | Paren of reg_exp (* I would accept it witht his case missing *)
  2. !G!i!v!e! !a! (most general) !u!n!i!f!i!e!r! !f!o!r! !t!h!e! !f!o!l!l!o!w!i!n!g! !u!n!i!f!i!c!a!t!i!o!n! !i!n!s!t!a!n!c!e!.! !Capital !l!e!tt!!e!r!s! ! d!e!n!o!t!e! !v!a!r!i!a!b!l!e!s! !o!f! !u!n!i!f! i!c!a!t!i!o!n!.! !S!h!o!w! !y!o!u!r! !w!o!r!k! !b!y! !l!i!s!t!i!n!g! !t!h!e! !o!p!e!r!a!t!i!o!n! !p!e!r!f!o!r!m!e!d! !i!n! ! e!a!c!h! !s!t!e!p! !o!f! !t!h!e! !u!n!i!f! i!c!a!t!i!o!n! !a!n!d! !t!h!e! !r!e!s!u!l!t! !o!f! !t!h!a!t! !s!t!e!p!.! !!! {X = f(g(x),W), h(y) = Y, f(Z,x) = f(Y,W)} Solution: 11:39 – 11: {X = f(g(x),W), h(y) = Y, f(Z,x) = f(Y,W)}  {h(y) = Y, f(Z,x) = f(Y,W)} with {X = f(g(x),W)} by eliminate  {Y = h(y), f(Z,x) = f(Y,W)} with {X = f(g(x),W)} by orient  {f(Z,x) = f(h(y),W)} with {X = f(g(x),W), Y = h(y)} by eliminate  {Z = h(y), x=W} with {X = f(g(x),W), Y = h(y)} by decompose  {x = W} with {X = f(g(x),W), Y = h(y), Z = h(y)} by eliminate  {W = x} with {X = f(g(x),W), Y = h(y), Z = h(y)} by orient answer: {X = f(g(x),x), Y = h(y), Z = h(y), W = x} by eliminate
  3. !I!n! !t!h!e! λ!-!e!x!p!r!e!s!s!i!o!n! !b!e!l!o!w!,! !w!r!i!t!e! !u!n!d!e!r! !e!a!c!h! !a!r!r!o!w! !w!h!e!t!h!e!r! !t!h!e! !i!n!d!i!c!a!t!e!d! !v!a!r!i!a!b!l!e! ! o!c!c!u!r!e!n!c!e! !i!s! !f!r!e!e! !o!r! !b!o!u!n!d! !(!!w!r!i!t!e! !!F !!!o!r! !!B !!)!. Also, for each bound occurrence, draw an arrow back to the abstraction that binds it. λ x. (z (λ y. λ x. y z x) (y x)) ↑ ↑ ↑ ↑ ↑ ↑ Solution: F B F B F B
  1. Evaluate the following λ!-!e!x!p!r!e!s!s!i!o!n to αβ-normal form, if one exists or explain why one does not exists. Show all work. (λ x. λ y. λ z. y z x) (λ x. x x) (λ x. λ y. y x) (λ x. x) Solution: (λ x. λ y. λ z. y z x) (λ x. x x) (λ x. λ y. y x) (λ x. x)  (λ y. λ z. y z (λ x. x x)) (λ x. λ y. y x) (λ x. x)  (λ z. (λ x. λ y. y x) z (λ x. x x)) (λ x. x)  (λ z. (λ y. y z ) (λ x. x x)) (λ x. x) (* Mistake here before *)  (λ z. (λ x. x x) z) (λ x. x)  (λ z. z z) (λ x. x)  (λ x. x)
  2. Evaluate the following λ!-!e!x!p!r!e!s!s!i!o!n as far as possible using each of lazy evaluation and eager evaluation. If either evaluation fails terminate, write “Diverges” and a brief explanation why the evaluation fails to terminate. (λ y. λ x. y x x) (λ x. λ y. x x) ((λ x. x (λ y. y )) (λ x. x)) Solution: Lazy Evaluation: (λ y. λ x. y x x) (λ x. λ y. x x) ((λ x. x (λ y. y )) (λ x. x))  (λ x. (λ x. λ y. x x) x x) ((λ x. x (λ y. y )) (λ x. x))  (λ x. λ y. x x) ((λ x. x (λ y. y )) (λ x. x)) ((λ x. x (λ y. y )) (λ x. x))  (λ y. ((λ x. x (λ y. y )) (λ x. x)) ((λ x. x (λ y. y )) (λ x. x))) ((λ x. x (λ y. y )) (λ x. x)) (left out)  ((λ x. x (λ y. y )) (λ x. x)) ((λ x. x (λ y. y )) (λ x. x))  ((λ x. x) (λ y. y )) ((λ x. x (λ y. y )) (λ x. x))  (λ y. y ) ((λ x. x (λ y. y )) (λ x. x))  (λ x. x (λ y. y )) (λ x. x)  (λ x. x) (λ y. y )  (λ y. y ) Eager Evaluation: (λ y. λ x. y x x) (λ x. λ y. x x) ((λ x. x (λ y. y )) (λ x. x))  (λ x. (λ x. λ y. x x) x x) ((λ x. x (λ y. y )) (λ x. x))  (λ x. (λ x. λ y. x x) x x) ((λ x. x) (λ y. y ))  (λ x. (λ x. λ y. x x) x x) (λ y. y ) (λ x. λ y. x x) (λ y. y ) (λ y. y )  (λ y. ((λ y. y ) (λ y. y))) (λ y. y )  (λ y. y ) (λ y. y ) (λ y. y )
  3. Represent the constructors for the following OCAML type in the lambda calculus type answer = yes | no | maybe Write the lambda term that returns the representation of yes when applied to the representation of no, the representation of no when applied to the representation of yes, and the representation of maybe when applied to the representation of maybe.