Debian Codegraph @

benchmark/tests/tailmerge.sml

raw
1 (* Written by Stephen Weeks ([email protected]). *)
2 fun merge (l1: int list, l2) =
3 let
4 fun revapp (l, acc) =
5 case l of
6 [] => acc
7 | h :: t => revapp (t, h :: acc)
8 fun loop (l1, l2, acc) =
9 case (l1, l2) of
10 ([], _) => revapp (acc, l2)
11 | (_, []) => revapp (acc, l1)
12 | (x1 :: l1', x2 :: l2') =>
13 if x1 <= x2
14 then loop (l1', l2, x1 :: acc)
15 else loop (l1, l2', x2 :: acc)
16 in
17 loop (l1, l2, [])
18 end
19
20 structure Main =
21 struct
22 fun doit size =
23 let
24 val len = 100000
25 val l1 = List.tabulate (len, fn i => i * 2)
26 val l2 = List.tabulate (len, fn i => i * 2 + 1)
27
28 fun test () =
29 if 0 = hd (merge (l1, l2))
30 then ()
31 else raise Fail "bug"
32
33 fun loop n =
34 if n = 0
35 then ()
36 else (test (); loop (n - 1))
37 in
38 loop size
39 end
40 end