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;;
Exercise 6.3 p. 157. "No conversion" means that the contents of the memory location (the sequence of 0s and 1s) is left unchanged.
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;
}
}