quiz3-soln-fall10

# quiz3-soln-fall10 - let attendance x = fold(fun a b> if b...

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

CMSC330 Fall 2010 Quiz #3 Solutions 1. (12 pts) OCaml a. (2 pts) Give the type of the following OCaml expression fun x y -> x (y+2) Type = (int -> ‘a) -> int -> ‘a b. (2 pts) Write an OCaml expression with the following type (bool -> int) -> int Code = fun x -> 1+(x true) fun x -> [1 ; (x true) ] let f x = 1 + (x true) let f x = [1 ; (x true) ] c. (2 pts) Give the value of the following OCaml expression. If an error exists, describe the error. (fun x -> if (x > 0) then x+1 ) 1 Value/Error = Error = missing else branch = unit (), so type of then branch must match Message = this expression has type int but is here used with type unit d. (6 pts) Using fold and an anonymous function, write a function attendance which when applied to a list lst of bools, returns the number of elements of lst that are true. Example: attendance [true; false; false; true; true] = 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: let attendance x = fold (fun a b -> if b then (a+1) else a) 0 x 2. (8 pts) Context free grammars a. (2 pts) Write a grammar for a x b y , where x = y+3 (i.e., exactly 3 more a’s than b’s) S → aaaL L → aLb | epsilon b. (6 pts) Consider the following grammar (S = start symbol & terminals = [ , ] , ; , e ): S → [ A ] | epsilon A → A ; S | e i. (3 pts) Present a derivation for the string [e;[e;]] S ⇒ [A] ⇒ [A;S] ⇒ [e;S] ⇒ [e;[A]] ⇒ [e;[A;S]] ⇒ [e;[e;S]] ⇒ [e;[e;]] leftmost S ⇒ [A] ⇒ [A;S] ⇒ [A;[A]] ⇒ [A;[A;S]] ⇒ [A;[A;]] ⇒ [A;[e;]] ⇒ [e;[e;]] rightmost ...(many other possible derivations) ii. (3 pts) Show the parse tree for your derivation let rec fold f a l = match l with -> a | (h::t) -> fold f (f a h) t S e [ A ] A ; S ε [ A ] A ; S e...
View Full Document

{[ snackBarMessage ]}

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