quiz2-soln-fall09

quiz2-soln-fall09 - CMSC330 Fall 2009 Quiz #2 Solution 1....

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

View Full Document Right Arrow Icon
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 list b. (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 = 0 2. (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 lst that 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
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.

Ask a homework question - tutors are online