-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwd.sml
62 lines (49 loc) · 1.25 KB
/
lwd.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(* List without duplicates. *)
signature LWD = sig
type t
type elem
val empty : t
val singleton : elem -> t
val append : t * t -> t
val delete : elem -> t -> t
val member : elem -> t -> bool
val to_list : t -> elem list
val foldl : (elem * 'a -> 'a) -> 'a -> t -> 'a
val foldr : (elem * 'a -> 'a) -> 'a -> t -> 'a
end
functor Lwd (Set : sig
type t
type elem
val empty : t
val singleton : elem -> t
val insert : elem -> t -> t
val member : elem -> t -> bool
val delete : elem -> t -> t
end) :> LWD where type elem = Set.elem = struct
type elem = Set.elem
type t = elem list * Set.t
val empty = ([], Set.empty)
fun singleton x = ([x], Set.singleton x)
fun append ((l1, s1), (l2, _)) =
let
fun f (x, acc) =
if Set.member x s1
then acc
else (#1 acc @ [x], Set.insert x (#2 acc))
in
foldl f (l1, s1) l2
end
fun delete' _ [] = []
| delete' x (y :: ys) =
if Set.member x (Set.singleton y)
then ys
else y :: delete' x ys
fun delete x (l, s) =
if Set.member x s
then (delete' x l, Set.delete x s)
else (l, s)
fun member x (_, s) = Set.member x s
fun to_list (l, _) = l
fun foldl f x (l, _) = List.foldl f x l
fun foldr f x (l, _) = List.foldr f x l
end