Docsity

Sample Questions for Midterm (CS 421 Fall 2006), Exams of Computer Science

Toggle sidebar
bg1
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?
2. What environment is in effect after each declaration in the code in Problem 1?
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.
4. 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);;
5. 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.)
6. 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.)
7. 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.
pf2

Partial preview of the text

Download Sample Questions for Midterm (CS 421 Fall 2006) and more Exams Computer Science in PDF only on Docsity!

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?
  2. What environment is in effect after each declaration in the code in Problem 1?
  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.
  4. 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);;
  5. 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.)
  6. 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.)
  7. 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.
  1. !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)}
  2. !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)) ↑ ↑ ↑ ↑ ↑ ↑
  3. 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)
  4. 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 to 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))
  5. 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.