En este manual se presentan ejemplos de las funciones de la librería de conjuntos Data.Set 0.5.5.1. En los ejemplos se supone que se han importado la siguiente librería

ghci> import Data.Set as S

1 El tipo de los conjuntos

ghci> let c1 = fromList [3,2,5,3]
ghci> :t c1
c1 :: Set Integer
ghci> c1
fromList [2,3,5]
ghci> let c2 = fromList [2,5,2,3]
ghci> c1 == c2
True

2 Construcción de conjuntos

ghci> empty
fromList []
ghci> singleton 5
fromList [5]

3 Inserción y eliminación de elementos

ghci> insert 7 (fromList [3,2,5])
fromList [2,3,5,7]
ghci> insert 2 (fromList [3,2,5])
fromList [2,3,5]
ghci> delete 3 (fromList [3,2,5,3,4])
fromList [2,4,5]
ghci> delete 7 (fromList [3,2,5,3,4])
fromList [2,3,4,5]

4 Información sobre conjuntos

ghci> S.null (fromList [])
True
ghci> S.null (fromList [5,2])
False
ghci> size (fromList [3,2,5,3,2,1])
4
ghci> member 5 (fromList [3,2,5])
True
ghci> member 7 (fromList [3,2,5])
False
ghci> notMember 7 (fromList [3,2,5])
True
ghci> notMember 5 (fromList [3,2,5])
False
ghci> lookupLT 5 (fromList [2,4,3,5,7])
Just 4
ghci> lookupLT 2 (fromList [2,4,3,5,7])
Nothing
ghci> lookupGT 2 (fromList [2,4,3,5,7])
Just 3
ghci> lookupGT 7 (fromList [2,4,3,5,7])
Nothing
ghci> lookupLE 5 (fromList [2,4,3,5,7])
Just 5
ghci> lookupLE 6 (fromList [2,4,3,5,7])
Just 5
ghci> lookupLE 1 (fromList [2,4,3,5,7])
Nothing
ghci> lookupGE 5 (fromList [2,4,3,5,7])
Just 5
ghci> lookupGE 6 (fromList [2,4,3,5,7])
Just 7
ghci> lookupGE 9 (fromList [2,4,3,5,7])
Nothing

5 Subconjuntos

ghci> isProperSubsetOf (fromList [3,5]) (fromList [3,2,5]) 
True
ghci> isProperSubsetOf (fromList [3,5,2]) (fromList [3,2,5]) 
False
ghci> isProperSubsetOf (fromList [3,7]) (fromList [3,2,5]) 
False
ghci> isSubsetOf (fromList [3,5]) (fromList [3,2,5]) 
True
ghci> isSubsetOf (fromList [3,5,2]) (fromList [3,2,5]) 
True
ghci> isProperSubsetOf (fromList [3,7]) (fromList [3,2,5]) 
False

6 Elementos mínimos y máximos

ghci> findMin (fromList [3,2,5])
2
ghci> findMin empty
*** Exception: Set.findMin: empty set has no minimal element
ghci> findMax (fromList [3,2,5,4])
5
ghci> findMax empty
*** Exception: Set.findMax: empty set has no maximal element
ghci> deleteMin (fromList [3,2,5])
fromList [3,5]
ghci> deleteMin empty
fromList []
ghci> deleteMax (fromList [3,2,5,4])
fromList [2,3,4]
ghci> deleteMax empty
fromList []
ghci> deleteFindMin (fromList [3,2,7,4])
(2,fromList [3,4,7])
ghci> deleteFindMax (fromList [3,2,7,4])
(7,fromList [2,3,4])
ghci> minView (fromList [3,2,7,4])
Just (2,fromList [3,4,7])
ghci> minView empty
Nothing
ghci> maxView (fromList [3,2,7,4])
Just (7,fromList [2,3,4])
ghci> maxView empty
Nothing

7 Operaciones con conjuntos

7.1 Unión

ghci> union (fromList [3,2,5]) (fromList [2,7,5])
fromList [2,3,5,7]
ghci> unions [fromList [3,2], fromList [2,5], fromList [3,5,7]]
fromList [2,3,5,7]

7.2 Intersección

ghci> intersection (fromList [3,2,5]) (fromList [2,7,5])
fromList [2,5]

7.3 Diferencia

ghci> difference (fromList [2,5,3]) (fromList [1,4,5])
fromList [2,3]
ghci> fromList [2,5,3] \\ fromList [1,4,5]
fromList [2,3]

8 Funciones de orden superior

8.1 Filtrados y particiones

ghci> S.filter even (fromList [3,2,5,7,8,6,9])
fromList [2,6,8]
ghci> partition even (fromList [3,2,5,7,8,6,9])
(fromList [2,6,8],fromList [3,5,7,9])

8.2 Aplicaciones ("map")

ghci> S.map (+2) (fromList [3,2,7])
fromList [4,5,9]

8.3 Plegados ("fold")

ghci> S.foldr (-) 0 (fromList [3,2,5,2])
4
ghci> 2-(3-(5-0))
4
ghci> S.foldl (-) 0 (fromList [3,2,5,2])
-10
ghci> ((0-2)-3)-5
-10
ghci> let c = fromList [1..2000000]
ghci> S.foldr (+) 0 c
2000001000000
(1.85 secs, 282641652 bytes)
ghci> S.foldr' (+) 0 c
2000001000000
(0.42 secs, 176228860 bytes)
ghci> let c = fromList [1..2000000]
ghci> S.foldl (+) 0 c
2000001000000
(1.80 secs, 277983108 bytes)
ghci> S.foldl' (+) 0 c
2000001000000
(0.39 secs, 172569700 bytes)

9 Conversiones

9.1 De conjuntos a listas

ghci> elems (fromList [3,2,5,2,1,5])
[1,2,3,5]
ghci> toList (fromList [3,2,5,2,1,5])
[1,2,3,5]
ghci> toAscList (fromList [3,2,5,2,1,5])
[1,2,3,5]
ghci> toDescList (fromList [3,2,5,2,1,5])
[5,3,2,1]

9.2 De listas a conjuntos

ghci> fromList [3,2,5,2,15]
fromList [2,3,5,15]
ghci> :t it
it :: Set Integer
ghci> size (fromAscList [1..1000000])
1000000
(0.48 secs, 141113268 bytes)
ghci> size (fromList [1..1000000])
1000000
(1.49 secs, 607884408 bytes)
ghci> size (fromAscList [1..4000000])
4000000
(3.15 secs, 561334996 bytes)
ghci> size (fromDistinctAscList [1..4000000])
4000000
(2.98 secs, 432857796 bytes)

10 Igualdad

ghci> fromList [3,2,5] == fromList [5,2,3,2]
True
ghci> fromList [3,2,5] == fromList [5,1,3,2]
False

11 División según un elemento

ghci> split 5 (fromList [3,2,5,1,7])
(fromList [1,2,3],fromList [7])
ghci> split 9 (fromList [3,2,5,1,7])
(fromList [1,2,3,5,7],fromList [])
ghci> split 0 (fromList [3,2,5,1,7])
(fromList [],fromList [1,2,3,5,7])
ghci> splitMember 5 (fromList [3,2,5,1,7])
(fromList [1,2,3],True,fromList [7])
ghci> splitMember 6 (fromList [3,2,5,1,7])
(fromList [1,2,3,5],False,fromList [7])

12 Índices

ghci> findIndex 5 (fromList [3,7,5,3])
1
ghci> findIndex 4 (fromList [3,7,5,3])
*** Exception: Set.findIndex: element is not in the set
ghci> lookupIndex 5 (fromList [3,7,5,3])
Just 1
ghci> lookupIndex 4 (fromList [3,7,5,3])
Nothing
ghci> elemAt 1 (fromList [3,7,5,3])
5
ghci> elemAt 9 (fromList [3,7,5,3])
*** Exception: Set.elemAt: index out of range
ghci> deleteAt 1 (fromList [3,7,5])
fromList [3,7]
ghci> deleteAt 4 (fromList [3,7,5])
fromList *** Exception: Set.deleteAt: index out of range

13 Conjuntos y árboles

ghci> putStrLn (showTree (fromList [1..5]))
4
+--2
|  +--1
|  +--3
+--5

ghci> putStrLn (showTree (fromList [1..7]))
4
+--2
|  +--1
|  +--3
+--6
   +--5
   +--7
ghci> putStrLn (showTreeWith True False (fromList [1..5]))
4
+--2
|  +--1
|  +--3
+--5

ghci> putStrLn (showTreeWith True True (fromList [1..5]))
4
|
+--2
|  |
|  +--1
|  |
|  +--3
|
+--5

ghci> putStrLn (showTreeWith False True (fromList [1..5]))
+--5
|
4
|
|  +--3
|  |
+--2
   |
   +--1

ghci> putStrLn (showTreeWith False False (fromList [1..5]))
+--5
4
|  +--3
+--2
   +--1