CMSC330 Fall 2009 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 = int -> int listb.(2 pts) Write an OCaml expression with the following type int -> bool Code = many possible solutions: let f x = x > 0 OR fun x -> x == 1 etc…c.(2 pts) Give the value of the following OCaml expression. If an error exists, describe the error. let x = (fun z -> z – 1) in x 1 Value = 02.(6 pts) OCaml Programming Using fold and an anonymous function, write a function numAdults which when applied to a list of ints lst,returns the number of elements of lstthat are 18 or over. Example: numAdults [17;18; 21;16; 25] = 3 let numAdults lst = fold (fun a y -> if (y >= 18) then (a+1) else a ) 0 lst OR let numAdults = fold (fun a y -> if (y >= 18) then (a+1) else a ) 0 // -2 points for not using anonymous function with fold
This is the end of the preview.
access the rest of the document.