module I1M.Tabla
(Tabla,
tabla,
valor,
modifica
) where
newtype Tabla i v = Tbl [(i,v)]
deriving Int -> Tabla i v -> ShowS
[Tabla i v] -> ShowS
Tabla i v -> String
(Int -> Tabla i v -> ShowS)
-> (Tabla i v -> String)
-> ([Tabla i v] -> ShowS)
-> Show (Tabla i v)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall i v. (Show i, Show v) => Int -> Tabla i v -> ShowS
forall i v. (Show i, Show v) => [Tabla i v] -> ShowS
forall i v. (Show i, Show v) => Tabla i v -> String
$cshowsPrec :: forall i v. (Show i, Show v) => Int -> Tabla i v -> ShowS
showsPrec :: Int -> Tabla i v -> ShowS
$cshow :: forall i v. (Show i, Show v) => Tabla i v -> String
show :: Tabla i v -> String
$cshowList :: forall i v. (Show i, Show v) => [Tabla i v] -> ShowS
showList :: [Tabla i v] -> ShowS
Show
tabla :: Eq i => [(i,v)] -> Tabla i v
tabla :: forall i v. Eq i => [(i, v)] -> Tabla i v
tabla = [(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl
valor :: Eq i => Tabla i v -> i -> v
valor :: forall i v. Eq i => Tabla i v -> i -> v
valor (Tbl []) i
_ = String -> v
forall a. HasCallStack => String -> a
error String
"fuera de rango"
valor (Tbl ((i
j,v
v):[(i, v)]
r)) i
i
| i
i i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
j = v
v
| Bool
otherwise = Tabla i v -> i -> v
forall i v. Eq i => Tabla i v -> i -> v
valor ([(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl [(i, v)]
r) i
i
modifica :: Eq i => (i,v) -> Tabla i v -> Tabla i v
modifica :: forall i v. Eq i => (i, v) -> Tabla i v -> Tabla i v
modifica (i, v)
p (Tbl []) = [(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl [(i, v)
p]
modifica p' :: (i, v)
p'@(i
i,v
_) (Tbl (p :: (i, v)
p@(i
j,v
_):[(i, v)]
r))
| i
i i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
j = [(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl ((i, v)
p'(i, v) -> [(i, v)] -> [(i, v)]
forall a. a -> [a] -> [a]
:[(i, v)]
r)
| Bool
otherwise = [(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl ((i, v)
p(i, v) -> [(i, v)] -> [(i, v)]
forall a. a -> [a] -> [a]
:[(i, v)]
r')
where Tbl [(i, v)]
r' = (i, v) -> Tabla i v -> Tabla i v
forall i v. Eq i => (i, v) -> Tabla i v -> Tabla i v
modifica (i, v)
p' ([(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl [(i, v)]
r)
instance (Eq i, Eq v) => Eq (Tabla i v) where
(Tbl []) == :: Tabla i v -> Tabla i v -> Bool
== (Tbl []) = Bool
True
(Tbl ((i
i,v
v):[(i, v)]
t)) == Tbl [(i, v)]
t' = (i, v) -> [(i, v)] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem (i
i,v
v) [(i, v)]
t' Bool -> Bool -> Bool
&&
[(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl [(i, v)]
t Tabla i v -> Tabla i v -> Bool
forall a. Eq a => a -> a -> Bool
== [(i, v)] -> Tabla i v
forall i v. [(i, v)] -> Tabla i v
Tbl [(i, v)
p | (i, v)
p <- [(i, v)]
t', (i, v)
p (i, v) -> (i, v) -> Bool
forall a. Eq a => a -> a -> Bool
/= (i
i,v
v)]
Tabla i v
_ == Tabla i v
_ = String -> Bool
forall a. HasCallStack => String -> a
error String
"No se da"