Answers to Self-Study Questions

Test Yourself #1

Here is informal type inference for let rec sumlist = λ L . if null(L) then 0 else plus(car(L))(sumlist(cdr(L)))
  1. The type of null is: α-list → bool.
  2. The type of 0 is: int.
  3. The type of plus is: int → int → int.
  4. The type of car is: α-list → α.
  5. The type of cdr is: α-list → α-list.
  6. The type of the condition part of an if must be: bool.
  7. The types of the then and else parts of an if must be the same (so that the whole expression has a well-defined type).
Point 1 tells us that the type of L must be: α-list. Point 3 tells us that the type of car(L) must be int, so α must be int. Points 2, 3, and 7 tell us that the type of the if is int, which (together with knowing that L is of type α-list, and α is int) means that the type of sumlist is int-list → int.

Test Yourself #2

The correct type for plus is the non-generic type

The correct type for cons is the generic type