midterm1-fall12-solutions

# 1 wrong answer to test 4 not interesting duplicate 1

• Notes
• 10

This preview shows page 2 - 7 out of 10 pages.

-1 wrong answer to test -4 not “interesting” (duplicate) -1 poor or no description (i.e. description just states what the test case is, not why it was interesting.) 2

Subscribe to view the full document.

(12 points) Step 4 is implementing the program . Fill in the body of the intersperse function to com- plete 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 intersperse (c:’a) (l:’a list) : ’a list = begin match l with | [] -> [] | [x] -> [x] | x::xs -> x::c::(intersperse c xs) end Grading scheme: no deduction for minor syntax errors -2 incorrect Nil case -3 incorrect Singleton case -5 Not recursing/pattern matching on correct list various other errors at discretion 3
2. List Processing (20 points) For each of the following programs, write the value computed for r : a. let rec h (l:int list) : int = begin match l with | [] -> 0 | x::xs -> x * (h xs) end let r : int = h [1;2;3] 0 b. let rec g (l:’a list) : ’a list = begin match l with | [] -> [] | [x] -> [x] | x::y::xs -> if x < y then x::(g (y::xs)) else y::(g (x::xs)) end let r : int list = g [1;3;2;0] [1;2;0;3] c. let rec f (p: ’a -> bool) (l:’a list) : ’a list * ’a list = begin match l with | [] -> ([], []) | x::xs -> let (l,r) = f p xs in if p x then (x::l, r) else (l, x::r) end let r : (int list * int list) = f ( fun (x:int) -> x > 0) [0;1;2;-3;4] ([1;2;4], [0;-3]) 4

Subscribe to view the full document.

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. let k (x: ’a list) : ’a list = fold ( fun (h:’a) (v:’a list) -> v @ [h]) [] x let r : int list = k [1;3;2;4] [4;2;3;1] e. let j (x : int list list) : int list = let transformer (l:int list) : int = fold ( fun (x:int) (v:int) -> x + v) 0 l in transform transformer x let r : int list = j [[1;2;3];[4;5];[]] [6;9;0] Grading scheme, each answer worth four points: no deduction for minor syntax errors 1 point if the value is of the correct type 2 points (part c) if structure is correct 4 points if completely correct Other minors errors -1 to -4 at discretion (e.g. -1 point per wrong list item/wrong order) 5
3. Types (16 points)

Subscribe to view the full document.

• Fall '09
• Self-balancing binary search tree, int list

{[ snackBarMessage ]}

###### "Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

### 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