m2-soln-spr10

# M2-soln-spr10 - CMSC330 Spring 2010 Midterm#2 Solutions 1(16 pts OCaml Types and Type Inference Give the type of the following OCaml expression a(2

This preview shows pages 1–2. Sign up to view the full content.

CMSC330 Spring 2010 Midterm #2 Solutions 1. (16 pts) OCaml Types and Type Inference Give the type of the following OCaml expression a. (2 pts) [[1 ; 2]] Type = int list list b. (3 pts) fun x -> 2::x Type = (int list) -> (int list) Write an OCaml expression with the following type c. (2 pts) int list -> int Code = fun (x::_) -> x+2 d. (4 pts) (int -> bool) -> int Code = fun x -> if (x 1) then 2 else 3 Give the value of the following OCaml expression. If an error exists, describe it e. (2 pts) if (1 < 2) then 3 Value = error since 3 must be of type ( ) f. (3 pts) let f x = f 2 in 1 Value = error since f is undefined 2. (14 pts) Higher order & anonymous functions A prefix sum is an operation on lists in which the n th element in the result list is obtained from the sum of the first n elements in the operand list. Using the following code for fold and an anonymous function, write a function prefixSum which given a list of ints, returns the prefix sum for the list. You are not allowed to use any helper functions or OCaml library functions, with the exception of List.rev (which reverses a list). Partial credit given for solutions which do not use fold.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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.

### Page1 / 4

M2-soln-spr10 - CMSC330 Spring 2010 Midterm#2 Solutions 1(16 pts OCaml Types and Type Inference Give the type of the following OCaml expression a(2

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online