module I1M.Conjunto
(Conj,
vacio,
esVacio,
pertenece,
inserta,
elimina
) where
newtype Conj a = Cj [a]
deriving Conj a -> Conj a -> Bool
(Conj a -> Conj a -> Bool)
-> (Conj a -> Conj a -> Bool) -> Eq (Conj a)
forall a. Eq a => Conj a -> Conj a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Conj a -> Conj a -> Bool
== :: Conj a -> Conj a -> Bool
$c/= :: forall a. Eq a => Conj a -> Conj a -> Bool
/= :: Conj a -> Conj a -> Bool
Eq
instance (Show a) => Show (Conj a) where
showsPrec :: Int -> Conj a -> ShowS
showsPrec Int
_ (Cj [a]
s) = [a] -> ShowS
forall a. Show a => [a] -> ShowS
showConj [a]
s
showConj :: Show a => [a] -> String -> String
showConj :: forall a. Show a => [a] -> ShowS
showConj [] String
cad = String -> ShowS
showString String
"{}" String
cad
showConj (a
x:[a]
xs) String
cad = Char -> ShowS
showChar Char
'{' (a -> ShowS
forall a. Show a => a -> ShowS
shows a
x ([a] -> ShowS
forall a. Show a => [a] -> ShowS
showl [a]
xs String
cad))
where showl :: [a] -> ShowS
showl [] String
cs = Char -> ShowS
showChar Char
'}' String
cs
showl (a
y:[a]
ys) String
cs = Char -> ShowS
showChar Char
',' (a -> ShowS
forall a. Show a => a -> ShowS
shows a
y ([a] -> ShowS
showl [a]
ys String
cs))
vacio :: Conj a
vacio :: forall a. Conj a
vacio = [a] -> Conj a
forall a. [a] -> Conj a
Cj []
esVacio :: Conj a -> Bool
esVacio :: forall a. Conj a -> Bool
esVacio (Cj [a]
xs) = [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs
pertenece :: Ord a => a -> Conj a -> Bool
pertenece :: forall a. Ord a => a -> Conj a -> Bool
pertenece a
x (Cj [a]
s) = a
x a -> [a] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x) [a]
s
inserta :: Ord a => a -> Conj a -> Conj a
inserta :: forall a. Ord a => a -> Conj a -> Conj a
inserta a
x (Cj [a]
s) = [a] -> Conj a
forall a. [a] -> Conj a
Cj (a -> [a] -> [a]
forall {a}. Ord a => a -> [a] -> [a]
agrega a
x [a]
s)
where agrega :: a -> [a] -> [a]
agrega a
x' [] = [a
x']
agrega a
x' s' :: [a]
s'@(a
y:[a]
ys) | a
x' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
y = a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a] -> [a]
agrega a
x' [a]
ys
| a
x' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y = a
x' a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
s'
| Bool
otherwise = [a]
s'
elimina :: Ord a => a -> Conj a -> Conj a
elimina :: forall a. Ord a => a -> Conj a -> Conj a
elimina a
x (Cj [a]
s) = [a] -> Conj a
forall a. [a] -> Conj a
Cj (a -> [a] -> [a]
forall {a}. Ord a => a -> [a] -> [a]
elimina' a
x [a]
s)
where elimina' :: a -> [a] -> [a]
elimina' a
_ [] = []
elimina' a
x' s' :: [a]
s'@(a
y:[a]
ys') | a
x' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
y = a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a] -> [a]
elimina' a
x' [a]
ys'
| a
x' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y = [a]
s'
| Bool
otherwise = [a]
ys'