






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







module Bst = struct type bst = Empty | Node of int * bst * bst
let empty = Empty (* empty binary search tree *)
let is_empty = function (* return true for empty bst *) Empty -> true | Node (_, _, _) -> false
let rec insert n = function (* insert n into binary search tree *) Empty -> Node (n, Empty, Empty) | Node (m, left, right) -> if m = n then Node (m, left, right) else if n < m then Node(m, (insert n left), right) else Node(m, left, (insert n right))
(* Implement the following functions val min : bst -> int val remove : int -> bst -> bst val fold : ('a -> int -> 'a) -> 'a -> bst -> 'a val size : bst -> int ) let rec min = ( return smallest value in bst ) let rec remove n t = ( tree with n removed ) let rec fold f a t = ( apply f to nodes of t in inorder ) let size t = ( # of non-empty nodes in t *) end
a. (3 pts) Is insert tail recursive? Explain why or why not. No, since the return value for recursive call to insert cannot be used as the return value of the original call to insert. The return value is used to create a Node data type first, and the Node value is returned. b. (3 pts) Implement min as a tail-recursive function. Raise an exception for an empty bst. Any reasonable exception is fine. let rec min = function Empty -> (raise (Failure "min")) | Node (m, left, right) -> if (is_empty left) then m else min left
c. (6 pts) Implement remove. The result should still be a binary search tree. let rec remove n = function Empty -> Empty | Node (m, left, right) -> if m = n then ( if (is_empty left) then right else if (is_empty right) then left else let x = min right in Node(x, left, remove x right) // OR // else let x = max left in // Node(x, remove x left, right) ) else if n < m then Node(m, (remove n left), right) else Node(m, left, (remove n right)) d. (6 pts) Implement fold as an inorder traversal of the tree so that the code List.rev (fold (fun a m -> m::a) [] t) will produce an (ordered) list of values in the binary search tree. let rec fold f a n = match n with Empty -> a | Node (m, left, right) -> fold f (f (fold f a left) m) right e. (3 pts) Implement size using fold. let size t = fold (fun a m -> a+1) 0 t
Similarly, the function parseF handles the grammar rules for F -> F! | U directly instead of rewriting the grammar, creating the following productions: F ->? B ->? You must identify where code corresponding to parseB was inserted directly in the code for parseF in the comments below: let rec parseF lr = (* parseF *)
class A { public void a( ) { … } } class B extends A { public void b( ) { … } } class C extends B { public void c( ) { … }} Explain why the following code is or is not legal a. int count(Set s) { … } … count(new TreeSet( )); Legal. Actual parameter type (Set) matches formal parameter type (Set) b. int count(Set s) { … } … count(new TreeSet( )); Illegal. Actual parameter type (Set) is not a subclass of formal parameter type (Set), even though B is a subclass of A. c. int count(Set s) { … } … count(new TreeSet( )); Legal. Type erasure will cause formal parameter type (TreeSet) to become TreeSet, which matches actual parameter type (Set). d. int count(Set s) { … } … count(new TreeSet( )); Legal. Actual parameter type (Set) matches formal parameter type (Set), since? matches A. e. int count(Set s) { … } … count(new TreeSet()); Legal. Actual parameter type (Set) matches formal parameter type (Set), since “? extends A” can match A and its subclasses B & C (classes that extend A, including A) f. int count(Set s) { … } … count(new TreeSet()); Illegal. Actual parameter type (Set) does not match formal parameter type (Set), since “? extends B” can match only B and its subclass C (classes that extend B, including B) g. int count(Set s) { for (A x : s) x.a( ); … } Legal. The actual parameter type (Set) indicates s contains elements of class B or its subclasses. So any element of s may be treated as an object of class B or its subclasses (e.g., C). The for loop treats elements of s as objects of class A, which is a superclass of B, and thus is legal (can use subclass in place of superclass). h. int count(Set s) { for (C x : s) x.c( ); … } Illegal. The actual parameter type (Set) indicates s contains elements of class B or its subclasses. So any element of s may be treated as an object of class B or its subclasses (e.g., C). The for loop treats elements of s as objects of class C, and is illegal since elements of s may be objects of class B (cannot use superclass in place of subclass). i. int count(Set s) { for (A x : s) x.a( ); … } Illegal. The actual parameter type (Set) indicates s contains elements of class B or its superclasses. So any element of s may be treated as an object of class B or its superclasses (e.g., A, Object). The for loop treats elements of s as objects of class A, and is illegal since elements of s may be objects of class Object (cannot use superclass in place of subclass). j. int count(Set s) { for (C x : s) x.c( ); … } Illegal. The actual parameter type (Set) indicates s contains elements of class B or its superclasses. So any element of s may be treated as an object of class B or its superclasses (e.g., A, Object). The for loop
public class MyBarrier { int num; // shared read-only data int current = 0; // shared modifiable data Lock lock = new ReentrantLock(); Condition ready = lock.newCondition();
public MyBarrier (int n) { num = n; }
public void enter( ) throws InterruptedException { lock.lock(); // prevent data race on current current++; // incr # of threads at barrier
if (current == num) { // enough threads at barrier ready.signalAll(); // wake up other threads } // continue execution else { while (current < num) { // wait for more threads to enter ready.await(); // sleep until enough threads enter } // use while ( ) in case reset( ) called } lock.unlock(); }
public void reset( ) { lock.lock(); // prevent data race on current current = 0; lock.unlock(); } }
1 hour Spanish Monday Jiménez 15%
1 hour Computers Tuesday CSIC 10%
30 minute Computers Friday AVW 5%
d. (* 1 3) = 3 Given: 1 = λf.λy.f y 3 = λf.λy.f (f (f y)) M * N = λx.(M (N x)) Proof: (* 1 3) // replacing * w/ encoding = λx.(1 (3 x)) // replacing 3 w/ encoding = λx.(1 (λf.λy.f (f (f y)) x)) // β-reduction: 1st^ f → x = λx.(1 (λy.x (x (x y)))) // replacing 1 w/ encoding = λx.((λf.λy.f y) (λy.x (x (x y)))) // β-reduction: 1st^ f w/ λy.x (x (x y)) = λx.(λy.(λy.x (x (x y)))) y // β-reduction: 1st^ y → y = λx.λy.x (x (x y)) // α-conversion: replace x with f = λf.λy.f (f (f y)) // apply encoding for 3 = 3 e. (+ 2 1) = 3 Given: 1 = λf.λy.f y 2 = λf.λy.f (f y) 3 = λf.λy.f (f (f y)) M + N = λx.λy.(M x)((N x) y) Proof: (+ 2 1) // replacing + w/ encoding = λx.λy.(2 x)((1 x) y) // replacing 2 w/ encoding = λx.λy.((λf.λy.f (f y)) x)((1 x) y) // β-reduction: 1st^ f → x = λx.λy.(λy.x (x y))((1 x) y) // replacing 1 w/ encoding = λx.λy.(λy.x (x y))(((λf.λy.f y) x) y) // β-reduction: 1st^ f → x = λx.λy.(λy.x (x y))((λy.x y) y) // β-reduction: 3rd^ y → y = λx.λy.(λy.x (x y))(x y) // β-reduction: 2nd^ y → x y = λx.λy.x (x (x y)) // α-conversion: replace x with f = λf.λy.f (f (f y)) // apply encoding for 3 = 3
f. (Y fact) 2 = 2 // you do not need to expand any operators except fact & Y Given: Y = λf.(λx.f (x x)) (λx.f (x x)) fact = λf. λn.if n = 0 then 1 else n * (f (n-1)) Proof: (Y fact) 2 // replacing Y w/ encoding = (λf.(λx.f (x x)) (λx.f (x x)) fact) 2 // β-reduction: 1st^ f → fact = (λx.fact (x x)) (λx.fact (x x)) 2 // β-reduction: 1st^ x → λx.fact (x x) = (fact ((λx.fact (x x)) (λx.fact (x x)))) 2 // apply encoding for (Y fact) // ((λx.fact (x x)) (λx.fact (x x))) → (Y fact) // we know this is the encoding for (Y fact) from 3rd^ line of proof = (fact (Y fact)) 2 // apply encoding for fact