midterm1-sp13-solutions

O.f takes wrong number of arguments •-1 if missing

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: 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 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 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). 7 4. Binary Search Trees (28 points total) Recall the definition of generic binary trees and the binary search tree insert function: type ’a tree = | Empty | Node of ’a tree * ’a * ’a tree let rec insert (t:’a tree) (n:’a) : ’a tree = begin match t with | Empty -> Node(Empty, n, Empty)...
View Full Document

{[ snackBarMessage ]}

Page5 / 10

o.f takes wrong number of arguments •-1 if missing ho...

This preview shows document pages 5 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online