Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / regression / kittmergesort.sml
CommitLineData
7f918cf1
CE
1(*kittmergesort.sml*)
2
3(* This is tmergesort taken from Paulson's book , page 99 *)
4
5(* The merge function has been modified slightly, to
6 traverse and rebuild both arguments fully, even when
7 the one argument is empty. This ensures that both
8 recursive calls of tmergesort in itself can put their
9 results in regions local to the body of tmergesort.
10
11 One can show that the maximum number of live list elements
12 is 3n, where n is the number of elements to be sorted.
13 For n=50000 this should give an approximate memory usage of
14 3 * 50.000 list elements * 5 words/list element * 4 bytes/word=
15 3Mb. The actual memory usage (run24d) is 4.5Mb. The remaining
16 1.5Mb is probably largely due to the fact that merge puts
17 stuff on the stack (as it is not tail recursive).
18
19*)
20
21exception Take and Drop
22
23fun take(0, _ ) = []
24 | take(n, x::xs) = x::take(n-1, xs)
25 | take(n, []) = raise Take
26
27fun drop(0, l) = l
28 | drop(n, x::xs) = drop(n-1, xs)
29 | drop(n, []) = raise Drop
30
31fun digit n = chr(ord #"0" + n)
32
33fun digits(n,acc) =
34 if n >=0 andalso n<=9 then digit n:: acc
35 else digits (n div 10, digit(n mod 10) :: acc)
36fun int_to_string(n) = implode(digits(n,[#"\n"]))
37
38fun snd(x,y) = y
39
40val a = 167
41val m = 2147
42fun nextrand(seed) =
43 let val t = a*seed
44 in t - (m*(t div m))
45 end
46
47fun randlist(n,seed,tail)=
48 if n=0 then (seed,tail)
49 else randlist(n-1, nextrand seed, seed::tail)
50
51
52fun length [] = 0
53 | length (_::xs) = 1+length xs
54
55fun merge([], ys) = (ys:int list)@[]
56 | merge(xs, []) = xs @[]
57 | merge(l as x::xs, r as y:: ys) =
58 if x<= y then x::merge(xs, r)
59 else y:: merge(l, ys)
60
61fun tmergesort [] = []
62 | tmergesort [x] = [x]
63 | tmergesort xs =
64 let val k = length xs div 2
65 in merge(tmergesort(take(k, xs)),
66 tmergesort(drop(k, xs)))
67 end
68
69
70val result =
71let
72 val n = 50000
73 val xs = snd(randlist(n,1,[]))
74 val _ = print "\n List generated\n"
75 fun report msg = print(msg^"\n")
76in
77 report "Doing tmergesort...";
78 tmergesort xs;
79 report("Sorted " ^ int_to_string n ^ " numbers\n")
80end
81