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

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

View Full Document Right Arrow Icon
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 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”.
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern