## Csci 4651 Problem set 6

#### Problem 1 (18 points): type inference.

Apply the type inference algorithm to the examples below. Show all your work. Write down the resulting type for the function `f` in each case (or demonstrate a type mismatch if the function does not have a valid type). Feel free to use the OCaml interpreter to check your types.

``````
(* Question 1 *)
let f x y = if x < 2 then x :: [] else x :: [y];;

(* Question 2 *)
let f x = fst x :: snd x ;;

(* Question 3 *)
let f x = match x with
(y, []) -> y
|(z, w) -> z + w;;

(* Question 4 *)
let rec f x = match x with
[] -> []
|y :: ys -> (not (y < 2)) :: f ys;;
``````

#### Problem 2 (4 points).

Exercise 6.3 p. 157. "No conversion" means that the contents of the memory location (the sequence of 0s and 1s) is left unchanged.

#### Problem 3 (8 points).

In the following Java program please point out all L-values (expressions that are used to denote a memory location) and R-values (expressions used to denote a value in memory).
``````
import java.awt.*;

public class LRValues {

public static void main(String [] args) {
int x = 0;
x++;
boolean y = (x == 0);
if (y) {
y = !y;
}
int [] A = {1, 2, 3};
for (int i = 0; i < 2; ++i) {
A[i] = A[i+1];
}
Point thePoint = new Point();
thePoint.x = thePoint.x + 1;
}

}
``````

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.