{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm1-sp13-solutions

# If you would like to use a helper function in your

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

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

View Full Document

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.

Unformatted text preview: 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 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. let separate (v:int) (lst : int list) : int list * int list = ho_separate ( fun x -> x >= v) lst Grading Scheme: •-1 for wrong comparison operator (such as equality) •-1 if missing lst argument •-3 if missing function argument •-2 if h.o.f. takes wrong number of arguments-2 if h....
View Full Document

{[ snackBarMessage ]}

### Page3 / 10

If you would like to use a helper function in your answer...

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

View Full Document
Ask a homework question - tutors are online