September 22, 1999 | Strong Typing | Slide #23 |
fun map(f, []) = [] | map(f,(h::t)) = f(h)::(map(f, t));
Compiler says to itself:
Arguments are f (which has some unknown type) and [] (which has type 'a list)
h must have type 'a and t must have type 'a list.
On the right, f is used as a function applied to h, so f must have type 'a -> 'b for some 'b
The return value from f is used in the :: operation with the return from map, so map must return a 'b list.
Everything else checks out OK
Compiler says:
val map = fn : ('a -> 'b) * 'a list -> 'b list
Next | Copyright © 1999 M-J. Dominus |