midterm1-sp12-blank

Hint vint list list x v x let r int list list k 12 e

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

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

Info iconThis 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
Background 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”. let
Background image of page 7

Info iconThis 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 ]}

Page5 / 11

hint vint list list x v x let r int list list k 12 e let...

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

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