September 22, 1999 Strong Typing Slide #25

Type Elaboration Example 5

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:



Next Copyright © 1999 M-J. Dominus