| September 22, 1999 | Strong Typing | Slide #25 | 
Sorry, I must do one more! The ML Type Aliens have control of my brain!
        fun foo []                = []
          | foo ((true,  ls)::t)  = (rev ls)::(foo t)
          | foo ((false, ls)::t)  =      ls ::(foo t) ;
        val foo = fn : (bool * 'a list) list -> 'a list list
Example:
        foo [(true,  [1,2,3]),
             (false, [4,5,6]),
             (true,  [7,8,9])] ;
                val it = 3,2,1], [4,5,6], [9,8,7 : int list list;
Equivalent formulation:
        fun foo []             = []
          | foo ((b,  ls)::t)  = (if b then (rev ls) else ls)::(foo t) ;
Side note:
If rev were an argument, it would have to have type 'z -> 'z.
In fact, it has type 'z list -> 'z list, which is suitable.
The compiler checks to make sure.
| Next | ![]()  | 
    Copyright © 1999 M-J. Dominus |