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)))
- The type of null is: α-list → bool.
- The type of 0 is: int.
- The type of plus is: int → int → int.
- The type of car is: α-list → α.
- The type of cdr is: α-list → α-list.
- The type of the condition part of an if must be: bool.
- 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
∀ α. α → (α-list → α-list)