{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

quiz2-soln-fall09

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

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

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
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Ask a homework question - tutors are online