midterm1-sp13-solutions

1 wrong answer to test 4 not interesting duplicate 1

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

-1 wrong answer to test -4 not “interesting” (duplicate) -1 poor or no description (i.e. description just states what the test case is “insert 3”, not why it was interesting.) (12 points) Step 4 is implementing the program . Fill in the body of the insert 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 insert (x: int) (xs : int list) : int list = begin match xs with | [] -> [ x ] | y :: ys -> if x <= y then x :: y :: ys else y :: insert x ys end Grading scheme: no deduction for minor syntax errors -1 wrong or missing type annotations -2 incorrect Nil case -2 incorrect test comparing x and y -2 (each) omitting x or y from insertion case -2 omitted y from noninsertion case -2 Not recursing on correct list -2 assuming the list contains no duplicates -2 insert in multiple cases various other errors at discretion 3

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

View Full Document
2. List recursion, higher-order functions and generic types (29 points total) This problem considers the following function, called separate . let rec separate (v:int) (lst : int list) : int list * int list = begin match lst with | [] -> ([],[]) | hd :: tl -> let (xs,ys) = separate v tl in if hd >= v then (xs, hd :: ys) else (hd :: xs, ys) end a. (9 points) Complete the following test cases for separate so that they return true. let test () : bool = separate 5 [] = _____________([],[])________________ let test () : bool = separate 5 [1;3;6;7] = ___([1;3],[6;7])____________ let test () : bool = separate 5 [1;5;6] = _______([1],[5;6])___________ Grading Scheme: 3 points per blank. No partial credit. No deduction for minor syntax errors, i.e. commas vs. semicolons as long as the answer is unambiguous. b. (9 points) Now consider a version of separate , called ho_separate , that takes a higher- order function as an additional argument. Here are two test cases for this version. let nonnegative (x:int):bool = x >= 0 let test () : bool = ho_separate nonnegative [-1; 1; 0; -2] = ([-1; -2],[1;0]) let positive (x:int):bool = x > 0 let test () : bool = ho_separate positive [-1; 0; 2; -2] = ([-1; 0; -2],[2]) 4
Fill in the blanks to complete the implementation of ho_separate . let rec ho_separate (f : _____int -> bool_______) (lst : int list) : int list * int list = begin match lst with | [] -> ([],[]) | hd :: tl -> let (xs,ys) = ___ho_separate f tl_____ in if _____f hd_______ then (xs, hd :: ys) else (hd :: xs, ys) end c. (4 points) Reimplement separate using ho_separate as a helper function. You should not use recursion—just call ho_separate with the appropriate arguments.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

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