Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh |
2 | * Jagannathan, and Stephen Weeks. | |
3 | * Copyright (C) 1997-2000 NEC Research Institute. | |
4 | * | |
5 | * MLton is released under a BSD-style license. | |
6 | * See the file MLton-LICENSE for details. | |
7 | *) | |
8 | ||
9 | structure ListPair: LIST_PAIR = | |
10 | struct | |
11 | exception UnequalLengths | |
12 | ||
13 | fun id x = x | |
14 | ||
15 | fun ul _ = raise UnequalLengths | |
16 | ||
17 | fun unzip l = | |
18 | List.foldr (fn ((x, y), (xs, ys)) => (x :: xs, y :: ys)) ([], []) l | |
19 | ||
20 | fun foldl' w f b (l1, l2) = | |
21 | let | |
22 | fun loop (l1, l2, b) = | |
23 | case (l1, l2) of | |
24 | ([], []) => b | |
25 | | (x1 :: l1, x2 :: l2) => loop (l1, l2, f (x1, x2, b)) | |
26 | | _ => w b | |
27 | in | |
28 | loop (l1, l2, b) | |
29 | end | |
30 | ||
31 | fun foldl f = foldl' id f | |
32 | ||
33 | fun foldlEq f = foldl' ul f | |
34 | ||
35 | fun foldr' w f b (l1, l2) = | |
36 | let | |
37 | fun loop (l1, l2) = | |
38 | case (l1, l2) of | |
39 | ([], []) => b | |
40 | | (x1 :: l1, x2 :: l2) => f (x1, x2, loop (l1, l2)) | |
41 | | _ => w b | |
42 | in | |
43 | loop (l1, l2) | |
44 | end | |
45 | ||
46 | fun foldr f = foldr' id f | |
47 | ||
48 | fun foldrEq f = foldr' ul f | |
49 | ||
50 | fun zip' w (l1, l2) = | |
51 | rev (foldl' w (fn (x, x', l) => (x, x') :: l) [] (l1, l2)) | |
52 | ||
53 | fun zip (l1, l2) = zip' id (l1, l2) | |
54 | ||
55 | fun zipEq (l1, l2) = zip' ul (l1, l2) | |
56 | ||
57 | fun map' w f = rev o (foldl' w (fn (x1, x2, l) => f (x1, x2) :: l) []) | |
58 | ||
59 | fun map f = map' id f | |
60 | ||
61 | fun mapEq f = map' ul f | |
62 | ||
63 | fun app' w f = foldl' w (fn (x1, x2, ()) => f (x1, x2)) () | |
64 | ||
65 | fun app f = app' id f | |
66 | ||
67 | fun appEq f = app' ul f | |
68 | ||
69 | fun exists p (l1, l2) = | |
70 | let | |
71 | fun loop (l1, l2) = | |
72 | case (l1, l2) of | |
73 | (x1 :: l1, x2 :: l2) => p (x1, x2) orelse loop (l1, l2) | |
74 | | _ => false | |
75 | in | |
76 | loop (l1, l2) | |
77 | end | |
78 | ||
79 | fun all p ls = not (exists (not o p) ls) | |
80 | ||
81 | fun allEq p = | |
82 | let | |
83 | fun loop (l1, l2) = | |
84 | case (l1, l2) of | |
85 | ([], []) => true | |
86 | | (x1 :: l1, x2 :: l2) => p (x1, x2) andalso loop (l1, l2) | |
87 | | _ => false | |
88 | in | |
89 | loop | |
90 | end | |
91 | end |