CIS
midterm1-sp12-blank

# Let rec k x int list int list list fold fun hint vint

• Notes
• 11

This preview shows pages 5–8. Sign up to view the full content.

let rec k (x: int list) : int list list = fold ( fun (h:int) (v:int list list) -> x :: v) [] x let r : int list list = k [1;2] e. let rec f (x : int list) : int list list = transform ( fun (h:int) -> h :: x) x let r : int list list = f [1;2] 5

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

3. Types (10 points) 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 Set1 module, which has the following abstract interface: module type Set = sig type ’a set val empty : ’a set val is_empty : ’a set -> bool val mem :’a ->’a set -> bool val add : ’a -> ’a set -> ’a set val union : ’a set -> ’a set -> ’a set val remove : ’a -> ’a set -> ’a set val list_to_set : ’a list -> ’a set val equal : ’a set -> ’a set -> bool val elements : ’a set -> ’a list end module Set1 : Set = ... ;; open Set1 let x : ______ int set ______ = add 3 empty let a : _____________________ = [2; "four"] let b : _____________________ = 2 :: 4 let c : _____________________ = (2,4) let d : _____________________ = add [3] empty let e : _____________________ = add 3 [1;2;3] let f : _____________________ = list_to_set [1;2;3] let g : _____________________ = fun (x : int) -> x + 1 let h : _____________________ = ( fun (x : int) -> x + 1) 10 let i : _____________________ = fun (f : int -> bool) -> f 3 let j : _____________________ = fun (x:’a set) -> add x empty 6
4. Binary Trees (25 points) Recall the definition of generic binary trees: type ’a tree = | Empty | Node of ’a tree * ’a * ’a tree 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) 3 1 3 3 2 / \ \ / \ / \ 2 4 2 2 2 1 3 / \ \ / \ 1 0 4 1 1 / 3 b. (8 points) For each definition below, circle the letter of the tree above that it constructs or “none of the above”.

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '09
• Self-balancing binary search tree, val empty, int list list

{[ 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