{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm1-sp13-solutions

Let separate vint lst int list int list int list

Info iconThis preview shows pages 5–8. Sign up to view the full content.

View Full Document Right Arrow Icon
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 -1 if missing ho separate other errors at discretion d. (3 points) Now consider a different version of separate , called generic_separate . Here are two test cases for generic_separate . let test () : bool = generic_separate "c" ["a";"b";"d"] = (["a";"b"],["d"]) let test () : bool = generic_separate 0.5 [0.0;0.7;0.2; 0.8] = ([0.0;0.2],[0.7;0.8]) (Note that generic_separate does not take a higher-order function as an argument.) What is the interface to this function? Write the type as it might appear in a .mli file. val generic_separate: ’a -> ’a list -> ’a list * ’a list Grading Scheme: 1 point partial credit for answer ’a -> ’a list -> ’a list . 5
Background image of page 5

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

View Full Document Right Arrow Icon
e. (4 points) Reimplement separate using generic_separate as a helper function. You should not use recursion—just call generic_separate with the appropriate arguments. let separate (v:int) (lst : int list) : int list * int list = generic_separate v lst Grading Scheme: -1 if missing lst argument -1 if missing v argument -1 if missing generic separate other errors at discrection 6
Background image of page 6
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 the most generic one. Recall that the @ operator appends two lists together in OCaml. We have done the first one for you. Consider the definitions to be below the following code: module type SET = sig type ’a set val fromList : ’a list -> ’a set end module LSet : SET = struct type ’a set = ’a list let fromList (l : ’a list) = l end open LSet;; let x : ______ string _____________ = "120 " ˆ "is fun" let a : ___________ill typed____________________ = "120" ˆ 120 let b : ___________ill typed_____________________ = [120] :: [120] let c : ____________int list_____________________ = 120 :: [120] let d : ___________int * int_____________________ = (120, 120) let e : ________(int * int) list__________________ = [(120, 120)] let f : _________int set____________________ = fromList [120] let g : _________int set____________________ = fromList ([2] @ [3]) let h : __________ill typed_________________ = (fromList [2]) @ (fromList [3]) Grading scheme: 2 points per answer: 0 if wrong, 2 if right. No deduction for omitting parens on (e).
Background image of page 7

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

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

{[ snackBarMessage ]}