midterm1-fall12-blank

If an expression can have multiple types give the

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 : _____________________________________ = (2::):: let b : _____________________________________ = 2 + "three" let c : _____________________________________ = add 3 true empty let d : _____________________________________ = add 3 true let e : _____________________________________ = mem 3 [1;2;3] let f : _____________________________________ = fun (g:int -> int) -> g 3 let g : _____________________________________ = fun (x:int) (y:int) -> x + y let h : _____________________________________ = add 3 (from_list [(1,2)]) empty 6 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 b. (12 points) For each definition below, circle the letter of the tree above that it constructs or “none of the above”....
View Full Document

{[ snackBarMessage ]}

Page6 / 10

If an expression can have multiple types give the most...

This preview shows document pages 6 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online