midterm1-sp12-blank

# Complete the following tests with examples of the

• Notes
• 11

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

Complete the following tests with examples of the expected behavior. We have done the first one for you. Note that some test cases are better than others, and credit will be assigned accordingly: make sure your tests cover a sufficiently broad range of “interesting” inputs. 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.

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 define 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.

The last two programs refer to the following definitions. 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.
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