{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm1-fall12-blank

Fill in the description string of the runtest

Info iconThis preview shows pages 2–7. Sign up to view the full content.

View Full Document Right Arrow Icon
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 = [1;0;2;0;3] = (interspserse 0 [1;2;3]) ;; run_test "comes from the problem description" test ii. let test () : bool = _________ = (intersperse _________ _______________________) ;; run_test "___________________________________________________" test iii. let test () : bool = _________ = (intersperse _________ _______________________) ;; run_test "___________________________________________________" test iv. let test () : bool = _________ = (intersperse _________ _______________________) ;; run_test "___________________________________________________" test 2
Background image of page 2

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

View Full Document Right Arrow Icon
(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:______________) (l:______________) : ______________ = 3
Background image of page 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] 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] 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] 4
Background image of page 4

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

View Full Document Right Arrow Icon
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] 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];[]] 5
Background image of page 5
3. Types (16 points) For each OCaml value or function definition below, fill in the blank where the type annotation could go or write “ill typed” if there is a type error. If an expression can have multiple types, give
Background image of page 6

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

View Full Document Right Arrow Icon
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}