midterm1-fall12-solutions

# For each ocaml value or function definition below

This preview shows pages 6–10. Sign up to view the full content.

For each OCaml value or function definition below, fill in the blank where the type annotation could go or write “ill typed” if there is a type error. If an expression can have multiple types, give the most generic one. We have done the first one for you. Some of these definitions refer to functions from the Map1 module, which has the following abstract interface: module type MAP = sig type (’a, ’b) map val empty : (’a, ’b) map val is_empty : (’a, ’b) map -> bool val mem : ’a -> (’a, ’b) map -> bool val find : ’a -> (’a, ’b) map -> ’b val add : ’a -> ’b -> (’a, ’b) map -> (’a, ’b) map val remove : ’a -> (’a, ’b) map -> (’a, ’b) map val from_list : (’a * ’b) list -> (’a, ’b) map val bindings : (’a, ’b) map -> (’a * ’b) list end module Map1 : MAP = struct ... end ;; open Map1 let x : ______ (int,string) map _____________ = add 120 "is fun" empty let a : _______int list list ________________ = (2::[])::[] let b : ________ill typed____________________ = 2 + "three" let c : ________(int, bool) map______________ = add 3 true empty let d : ___(int,bool) map -> (int, bool) map_ = add 3 true let e : ____ill typed________________________ = mem 3 [1;2;3] let f : ____(int -> int) -> int______________ = fun (g:int -> int) -> g 3 let g : _____int -> int -> int_______________ = fun (x:int) (y:int) -> x + y let h : _____(int,(int,int) map) map_________ = add 3 (from_list [(1,2)]) empty Grading scheme: 2 points per answer: 0 if wrong, 2 if right. 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4. Binary Search Trees (17 points) Recall the definition of generic binary trees and the binary search tree insert function: type ’a tree = | Empty | Node of ’a tree * ’a * ’a tree let rec insert (t:’a tree) (n:’a) : ’a tree = begin match t with | Empty -> Node(Empty, n, Empty) | Node(lt, x, rt) -> if x = n then t else if n < x then Node (insert lt n, x, rt) else Node(lt, x, insert rt n) end a. (5 points) Circle the trees that satisfy the binary search tree invariant . (Note that we have omitted the Empty nodes from these pictures.) (a) (b) (c) (d) (e) 4 2 2 2 2 / \ / \ / \ \ \ 2 5 5 6 5 6 5 5 \ / \ / \ / \ 6 4 4 4 6 4 4 Answer: (a), (d) b. (12 points) For each definition below, circle the letter of the tree above that it constructs or “none of the above”. let t1 : int tree = insert (Node(Node(Empty, 5 Empty), 2, Node(Empty, 6, Empty))) 4 (a) (b) (c) (d) (e) none of the above Answer: (b) let t2 : int tree = insert (insert (insert (insert Empty 4) 2) 5) 6 (a) (b) (c) (d) (e) none of the above Answer: (a) let t3 : int tree = insert (insert (insert (insert Empty 2) 5) 4) 6 (a) (b) (c) (d) (e) none of the above Answer: (d) 7
let t4 : int tree = insert (insert (insert (insert Empty 5) 2) 4) 6 (a) (b) (c) (d) (e) none of the above Answer: none of the above 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5. Lists and Binary Trees (20 points)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern