{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm1-fall12-solutions

For each ocaml value or function definition below

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

View Full Document Right Arrow Icon
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
Background image of page 6

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

View Full Document Right Arrow Icon
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
Background image of page 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
Background image of page 8

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

View Full Document Right Arrow Icon
5. Lists and Binary Trees (20 points)
Background image of page 9
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}