September 22, 1999 Strong Typing Slide #28

Splitting

        fun split []        = ([], [])
          | split [h]       = ([h], [])
          | split (x::y::t) = let val (s1, s2) = split t
                               in (x::s1, y::s2)
                              end
        val split = fn : 'a list -> 'a list * 'a list

Merging

        fun merge ([], x) = x:int list
          | merge (x, []) = x
          | merge (h1::t1, h2::t2) = 
            if h1 < h2    then h1::merge(    t1, h2::t2)
                          else h2::merge(h1::t1,     t2);
        val merge = fn : int list * int list -> int list

(Also the type checker caught a missing comma error while I was writing the notes.)


Next Copyright © 1999 M-J. Dominus