1 (* Copyright (C
) 1999-2006 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
3 * Copyright (C
) 1997-2000 NEC Research Institute
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
13 datatype list
= datatype Primitive
.List.list
36 | x
:: r
=> SOME (x
, r
)
43 | x
:: l
=> loop (l
, f (x
, b
))
47 fun length l
= foldl (fn (_
, n
) => n
+?
1) 0 l
49 fun appendRev (l1
, l2
) = foldl (op ::) l2 l1
51 val revAppend
= appendRev
53 fun rev l
= appendRev (l
, [])
58 | _
=> appendRev (rev l1
, l2
)
60 fun foldr f b l
= foldl f
b (rev l
)
62 fun concat ls
= foldr (op @
) [] ls
64 fun app f
= foldl (f
o #
1) ()
66 fun map f l
= rev (foldl (fn (x
, l
) => f x
:: l
) [] l
)
68 fun mapPartial pred l
=
69 rev (foldl (fn (x
, l
) => (case pred x
of
74 fun filter pred
= mapPartial (fn x
=> if pred x
then SOME x
else NONE
)
76 fun partition pred l
=
79 foldl (fn (x
, (trues
, falses
)) =>
80 if pred x
then (x
:: trues
, falses
)
81 else (trues
, x
:: falses
))
101 fun all pred
= not
o (exists (not
o pred
))
103 fun tabulate (n
, f
) =
104 if Primitive
.Controls
.safe
andalso n
< 0
109 then loop (i
+ 1, f i
:: ac
)
118 [] => raise Subscript
124 if Primitive
.Controls
.safe
andalso n
< 0
131 fun loop (l
, n
, ac
) =
134 [] => raise Subscript
135 | x
:: l
=> loop (l
, n
- 1, x
:: ac
))
138 if Primitive
.Controls
.safe
andalso n
< 0
148 [] => raise Subscript
149 | _
:: l
=> loop (l
, n
- 1))
152 if Primitive
.Controls
.safe
andalso n
< 0
163 |
(x1
::l1
,x2
::l2
) => (case cmp (x1
, x2
) of
164 EQUAL
=> loop (l1
, l2
)
170 structure ListGlobal
: LIST_GLOBAL
= List