September 22, 1999 | Strong Typing | Slide #18 |
fun sumof [] = 0 | sumof (h::t) = h + sumof t;
Compiler says to itself:
Argument is [] , so must be some kind of list, say 'a list
h::t is also a list, and so h must have type 'a and t must have type 'a list
The return value is 0, that's an int.
We're adding h to the return value of sumof, which is an int
h must also be an int -- now we know what 'a is.
t and h::t must be int list
Everything else checks out OK
Then it prints:
val sumof = fn : int list -> int
If we had written 0.0 instead of 0, the compiler would have deduced real list -> real.
If we had written true instead of 0, the compiler would have
reported a type error.
Next | Copyright © 1999 M-J. Dominus |