midterm1-sp12-blank

# Fill in the description string of the runtest

This preview shows pages 2–6. Sign up to view the full content.

Fill in the description string of the run_test function with a short explanation of why the test case is interesting. i. let test () : bool = [2;4] = (intersect [1;2;3;4] [0;2;4;5]) ;; run_test "comes from the problem description" test ii. let test () : bool = _________ = (intersect ________________ ________________) ;; run_test "___________________________________________________" test 2

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

View Full Document
iii. let test () : bool = _________ = (intersect ________________ ________________) ;; run_test "___________________________________________________" test iv. let test () : bool = _________ = (intersect ________________ ________________) ;; run_test "___________________________________________________" test (12 points) Step 4 is implementing the program . Fill in the body of the intersect function to complete the design. Do not use any list library functions (such as fold , or @ ) to solve this problem. If you would like to use a helper function in your answer, you must deﬁne it. let rec intersect (l1:______________) (l2:______________) : ______________ = 3
2. List Processing (20 points) For each of the following programs, write the value computed for r : a. let rec z (x:int list) : int list list = begin match x with | [] -> [ [] ] | _::t -> x :: (z t) end let r : int list list = z [1;2;3] b. let rec g (f:’a -> ’a list) (x:’a list) : ’a list = begin match x with | [] -> [] | h::t -> f h @ g f t end let r : int list = g ( fun (x:int) -> [x;x]) [1;2;3] c. let rec m (x:int option list) : int list = begin match x with | [] -> [] | (Some y)::t -> y :: m t | None :: t -> m t end let r : int list = m [Some 1; None; Some 2] 4

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

View Full Document
The last two programs refer to the following deﬁnitions. let rec transform (f: ’a -> ’b) (x: ’a list): ’b list = begin match x with | [] -> [] | h :: t -> (f h) :: (transform f t) end let rec fold (combine: ’a -> ’b -> ’b) (base: ’b) (x: ’a list): ’b = begin match x with | [] -> base | h :: t -> combine h (fold combine base t) end d. let rec k (x: int list) : int list list = fold ( fun (h:int) (v:int list list) -> x :: v) [] x
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page2 / 11

Fill in the description string of the runtest function with...

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

View Full Document
Ask a homework question - tutors are online