{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

quiz2-soln-spr10 - let getFirsts lst = map(fun a> match a...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC330 Spring 2010 Quiz #2 Solution 1. (6 pts) OCaml Types and Type Inference a. (2 pts) Give the type of the following OCaml expression fun x -> (x,2) Type = ‘a -> (‘a * int) b. (2 pts) Write an OCaml expression with the following type int -> (float * int list) Code = Example solutions: let f x = (2.0, [x+3]) etc… c. (2 pts) Give the value of the following OCaml expression. If an error exists, describe the error. (fun z -> fun y -> z - y) 5 3 Value = 2 2. (8 pts) OCaml Programming Using the following code for either map/fold and an anonymous function, write a function getFirsts which given a list of pairs, returns a list of the 1 st members of each pair as a list (in original or reverse order). Partial credit given for solutions which do not use map/fold. Example: getFirsts [(1,2);(3,4);(3,5)] = [1;3;3] OR [3;3;1] getFirsts [(“a”,”x”);(“b”,”y”);(“c”,”z”)] = [“a”;”b”;”c”] OR [“c”;”b”;”a”]
Image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: let getFirsts lst = map (fun a -> match a with (h,t) -> h) lst OR let getFirsts lst = map (fun (h,t) -> h) lst OR let getFirsts lst = fold (fun a y -> match y with (h,t) -> h::a) lst OR let getFirsts lst = fold (fun a (h,t) -> h::a) lst etc… 3. (6 pts) Context free grammars Consider the following grammar: S & aSaS | ± (* epsilon *) a. (2 pts) Describe the set of strings generated by the grammar. Strings of even numbers of a’s. I.e., (aa)* b. (4 pts) Is the grammar ambiguous? Show proof if possible. Grammar is ambiguous since there are 2 leftmost derivations for “aaaa”. S ± aSaS ± aaS ²± aaaSaS ± aaaaS ± aaaa S ± aSaS ± aaSaSaS ± aaaSaS ± aaaaS ± aaaa let rec map f l = match l with -> | (h::t) -> (f h)::(map f t) let rec fold f a l = match l with -> a | (h::t) -> fold f (f a h) t...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern