Ordenar tupla por campo fecha
Publicado por Ana (1 intervención) el 11/02/2013 14:31:57
Hola tengo el siguiente código en Haskell y quiero ordenarlo por fecha:
{-
Agenda
November 29, 2012
-}
data Day = D Int deriving Eq
data Month = M Int deriving Eq
data Year = Y Int deriving Eq
data Date = DT Day Month Year deriving Eq
type Note = String
lista_ordenada::Ord a=>[a]->[a]
lista_ordenada [] = []
lista_ordenada [_] = []
lista_ordenada (x:y:xs) = (x<=y) && lista_ordenada (y:xs)
data Agenda = AG [(Date,[Note])]
months = ["January","February","March","April","May","June",
"July","August","Septembet","October","November","December"]
instance Show Day where
show (D d) = show d
instance Show Month where
show (M m) = take 3 (months !! (m-1))
instance Show Year where
show (Y y) = show y
sepDate = '/'
instance Show Date where
show (DT d m a) =
show d ++ sepDate:(show m) ++ sepDate:show a
showNote = ('\t':)
showNotes = concat . (map (('\n':).showNote))
instance Ord Date Agenda where
show (AG []) = ""
show (AG ((date,notes):as)) =
show date ++ (showNotes notes) ++ '\n':show (AG as)
agenda = lista_ordenada[(DT (D 25) (M 07) (Y 2013),["Cumple","regalos"]),(DT (D 15) (M 12) (Y 2011),["Christmas","Travel"]),(DT (D 05) (M 10) (Y 2012),["mayo","alergia"])]
{-
instance Show Agenda where
show (AG []) = ""
show (AG ((date,notes):as)) =
show date ++ (showNotes notes) ++ '\n':show (AG as)
agenda = AG lista_ordenada[(DT (D 25) (M 07) (Y 2013),["Cumple","regalos"]),(DT (D 15) (M 12) (Y 2011),["Christmas","Travel"]),(DT (D 05) (M 10) (Y 2012),["mayo","alergia"])]
-}
--
-- writeNote :: Agenda -> Date -> Note -> Agenda
--
writeNote (AG as) date note =
case (lookup date as) of
Nothing -> AG ((date,[note]):as)
Just notes -> AG (before++(date,note:notes):after)
where (before,(_,notes):after) = break ((date==).fst) as
--
-- searchNotes :: Agenda -> Date -> [Note]
--
searchNotes (AG as) date =
case (lookup date as) of
Nothing -> ["No annotation"]
Just notes -> notes
Alguna idea? Gracias!
{-
Agenda
November 29, 2012
-}
data Day = D Int deriving Eq
data Month = M Int deriving Eq
data Year = Y Int deriving Eq
data Date = DT Day Month Year deriving Eq
type Note = String
lista_ordenada::Ord a=>[a]->[a]
lista_ordenada [] = []
lista_ordenada [_] = []
lista_ordenada (x:y:xs) = (x<=y) && lista_ordenada (y:xs)
data Agenda = AG [(Date,[Note])]
months = ["January","February","March","April","May","June",
"July","August","Septembet","October","November","December"]
instance Show Day where
show (D d) = show d
instance Show Month where
show (M m) = take 3 (months !! (m-1))
instance Show Year where
show (Y y) = show y
sepDate = '/'
instance Show Date where
show (DT d m a) =
show d ++ sepDate:(show m) ++ sepDate:show a
showNote = ('\t':)
showNotes = concat . (map (('\n':).showNote))
instance Ord Date Agenda where
show (AG []) = ""
show (AG ((date,notes):as)) =
show date ++ (showNotes notes) ++ '\n':show (AG as)
agenda = lista_ordenada[(DT (D 25) (M 07) (Y 2013),["Cumple","regalos"]),(DT (D 15) (M 12) (Y 2011),["Christmas","Travel"]),(DT (D 05) (M 10) (Y 2012),["mayo","alergia"])]
{-
instance Show Agenda where
show (AG []) = ""
show (AG ((date,notes):as)) =
show date ++ (showNotes notes) ++ '\n':show (AG as)
agenda = AG lista_ordenada[(DT (D 25) (M 07) (Y 2013),["Cumple","regalos"]),(DT (D 15) (M 12) (Y 2011),["Christmas","Travel"]),(DT (D 05) (M 10) (Y 2012),["mayo","alergia"])]
-}
--
-- writeNote :: Agenda -> Date -> Note -> Agenda
--
writeNote (AG as) date note =
case (lookup date as) of
Nothing -> AG ((date,[note]):as)
Just notes -> AG (before++(date,note:notes):after)
where (before,(_,notes):after) = break ((date==).fst) as
--
-- searchNotes :: Agenda -> Date -> [Note]
--
searchNotes (AG as) date =
case (lookup date as) of
Nothing -> ["No annotation"]
Just notes -> notes
Alguna idea? Gracias!
Valora esta pregunta


0