Docsity

Solutions for CS 421 Midterm: OCaml Coding Questions and Type Inference, Exams of Computer Science

Toggle sidebar
bg1
Solutions for Sample Questions for Midterm (CS 421)
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. 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);;
3. 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))
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
pf3
pf4
pf5

Partial preview of the text

Download Solutions for CS 421 Midterm: OCaml Coding Questions and Type Inference and more Exams Computer Science in PDF only on Docsity!

Solutions for Sample Questions for Midterm (CS 421)

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. 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);;
  3. 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)) 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.

  1. Using the rules provided in class, derive a valid type judgment for let rec fact n = if n = 0 then 1 else let r = fact (n - 1) in n * r;; (The rules will be provided for you on the exam, if this kind of question is asked.) Solution: This problem was a poor choice since it uses syntactic sugar. Also it mixes declarations with expressions. This won’t happen on the exam. I will modify the problem to be doable with what I showed in class. (I didn’t just write the tree because it wouldn’t fit.) By the let_rec rule we have (1) {fact : int -> int} |- (fun n -> if n = 0 then 1 else let r = fact (n – 1) in n * r) : int -> int (2) {fact : int -> int} |- fact : int -> int {} |- let rec fact = fun n -> if n = 0 then 1 else let r = fact (n – 1) in n * r in fact : int -> int (2) is valid by the variable rule: (2) {fact : int -> int} |- fact : int -> int By the fun rule we have (3) {n : int, fact : int -> int} |- (if n = 0 then 1 else let r = fact (n – 1) in n * r) : int (1) {fact : int -> int} |- (fun n -> if n = 0 then 1 else let r = fact (n – 1) in n * r) : int -> int By the if_then_else rule we have (4) {n : int, fact : int -> int} |- (n = 0) : bool (5) {n : int, fact : int -> int} |- 1:int (6) {n : int, fact : int -> int} |- (let r = fact (n – 1) in n * r) : int (3) {n : int, fact : int -> int} |- (if n = 0 then 1 else let r = fact (n – 1) in n * r) : int (5) is valid by the rule for constants. By the rule for binary relations we have (7) {n : int, fact : int -> int} |- n : int (8) ) {n : int, fact : int -> int} |- 0 : int (4) {n : int, fact : int -> int} |- (n = 0) : bool (7) is valid by the rule for variables. (8) is valid by the rule for constants. By the rule for let, we have (9) {n:int, fact:int -> int} |- fact (n – 1) : int (10) {r:int, n:int, fact:int -> int} |- (n * r) : int (6) {n : int, fact : int -> int} |- (let r = fact (n – 1) in n * r) : int By the rule for applications we have (11) {n:int, fact:int -> int} |- fact : int -> int (12){n:int, fact:int -> int} |- (n – 1) : int (9) {n:int, fact:int -> int} |- fact (n – 1) : int (11) is valid by the variable rule. By the rule for binary operations, we have
  1. !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
  2. 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)  (λ y. y (λ x. x x)) (λ x. x)  (λ x. x) (λ x. x x)  (λ x. x x)
  3. 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))  ((λ 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 )

  1. 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. Solution: yes  λ x. λ y. λ z. x no  λ x. λ y. λ z. y maybe  λ x. λ y. λ z. z The flipping function: λ a. a (λ x. λ y. λ z. y) (λ x. λ y. λ z. x) (λ x. λ y. λ z. z)
  2. For each of the following languages, give a regular expression describing it: a. The set of all binary strings with at least three ones b. The set of all binary strings with exactly one occurrence of 111 c. The set of all binary strings with an even number of zeros. Solution: a. 010101(0∨1) b. (0(ε∨ 1 ∨11)00)111(00(ε∨ 1 ∨11)0) c. 1* ∨ (10101)
  3. For each of the following finite state automata list each of the following regular expressions that describes the same language: a. f(g∨f)* b. ffg(g∨f) c. (g∨f)ffg(g∨f)* d. gf(f)g(g∨f)* e. fff(g∨f)* Solution: I: c, d II: b, III: a,e I: III: II: g,f g,f g,f g g f f f g f f g,f g,f g g