2012-10-10 21:37:42 +00:00
|
|
|
|
2012-10-18 09:19:50 +00:00
|
|
|
module Set (empty,singleton,insert,remove
|
|
|
|
,member
|
2012-10-19 07:13:28 +00:00
|
|
|
,foldl,foldr,map
|
2012-10-18 09:19:50 +00:00
|
|
|
,union,intersect,diff
|
|
|
|
,toList,fromList
|
|
|
|
) where
|
2012-10-10 21:37:42 +00:00
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
import Dict as Dict
|
|
|
|
import List as List
|
2013-02-22 23:19:40 +00:00
|
|
|
|
2013-02-08 04:23:25 +00:00
|
|
|
type Set t = Dict t ()
|
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Create an empty set.
|
2013-02-08 04:23:25 +00:00
|
|
|
empty : Set t
|
2012-10-16 06:03:14 +00:00
|
|
|
empty = Dict.empty
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Create a set with one value.
|
2013-02-08 04:23:25 +00:00
|
|
|
singleton : t -> Set t
|
2012-10-16 06:03:14 +00:00
|
|
|
singleton k = Dict.singleton k ()
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Insert a value into a set.
|
2013-02-08 04:23:25 +00:00
|
|
|
insert : t -> Set t -> Set t
|
2012-10-16 06:03:14 +00:00
|
|
|
insert k = Dict.insert k ()
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Remove a value from a set. If the value is not found, no changes are made.
|
2013-02-08 04:23:25 +00:00
|
|
|
remove : t -> Set t -> Set t
|
2012-10-16 06:03:14 +00:00
|
|
|
remove = Dict.remove
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Determine if a value is in a set.
|
2013-02-08 04:23:25 +00:00
|
|
|
member : t -> Set t -> Bool
|
2012-10-16 06:03:14 +00:00
|
|
|
member = Dict.member
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Get the union of two sets. Keep all values.
|
2013-02-08 04:23:25 +00:00
|
|
|
union : Set t -> Set t -> Set t
|
2012-10-18 09:19:50 +00:00
|
|
|
union = Dict.union
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Get the intersection of two sets. Keeps values that appear in both sets.
|
2013-02-08 04:23:25 +00:00
|
|
|
intersect : Set t -> Set t -> Set t
|
2012-10-18 09:19:50 +00:00
|
|
|
intersect = Dict.intersect
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Get the difference between the first set and the second. Keeps values that do not appear in the second set.
|
2013-02-08 04:23:25 +00:00
|
|
|
diff : Set t -> Set t -> Set t
|
2012-10-18 09:19:50 +00:00
|
|
|
diff = Dict.diff
|
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Convert a set into a list.
|
2013-02-08 04:23:25 +00:00
|
|
|
toList : Set t -> [t]
|
2012-10-18 09:19:50 +00:00
|
|
|
toList = Dict.keys
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Convert a list into a set, removing any duplicates.
|
2013-02-08 04:23:25 +00:00
|
|
|
fromList : [t] -> Set t
|
2013-03-10 09:07:17 +00:00
|
|
|
fromList xs = List.foldl (\k t -> Dict.insert k () t) empty xs
|
2012-10-18 09:19:50 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Fold over the values in a set, in order from lowest to highest.
|
2013-02-08 04:23:25 +00:00
|
|
|
foldl : (a -> b -> b) -> b -> Set a -> b
|
2013-03-10 09:07:17 +00:00
|
|
|
foldl f b s = Dict.foldl (\k _ b -> f k b) b s
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Fold over the values in a set, in order from highest to lowest.
|
2013-02-08 04:23:25 +00:00
|
|
|
foldr : (a -> b -> b) -> b -> Set a -> b
|
2013-03-10 09:07:17 +00:00
|
|
|
foldr f b s = Dict.foldr (\k _ b -> f k b) b s
|
2013-02-08 04:23:25 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Map a function onto a set, creating a new set with no duplicates.
|
2013-02-08 04:23:25 +00:00
|
|
|
map : (a -> b) -> Set a -> Set b
|
2013-03-24 12:45:56 +00:00
|
|
|
map f s = fromList (List.map f (toList s))
|