Enunciado aqui
module Exame where
data Tree a = Empty | Node a (Tree a) (Tree a)
type Extracto = [Movimento]
type Movimento = (Descricao, Tipo, Data, Montante)
type Descricao = String
data Tipo = Credito | Debito deriving (Eq,Show)
type Ano = Int
type Mes = Int
type Dia = Int
data Data = D Ano Mes Dia
deriving (Eq,Ord,Show)
type Montante = Double -- valor positivo
-- Dados para teste
movimento1=("Compra", Debito, (D 1999 12 01), 123.0)
movimento2=("EuroMilhoes", Credito, (D 2009 08 23), 155.5)
movimento3=("EDP", Debito, (D 2009 01 26), 32.0)
extracto = [movimento1,movimento2,movimento3]
-------------------------------
--pg1
menoresMaiores :: Float -> Tree Float -> ([Float],[Float])
menoresMaiores n Empty = ([],[])
menoresMaiores n arv = let l = toList arv
in
((filter (n>) l),(filter (n<) l)) where toList Empty = [] toList (Node n esq dir) = [n] ++ toList esq ++ toList dir --pg2 ePrefixo :: String -> String -> Bool
ePrefixo [] r = True
ePrefixo l [] = True
ePrefixo (x:xs) (y:ys) | (x==y)= True && ePrefixo xs ys
| otherwise = False
--pg3
procura :: Descricao -> Extracto -> [(Tipo,Data,Montante)]
procura des ext = map (\(x,y,w,z)->(y,w,z)) (filter (\(x,y,w,z)->(des==x)) ext)
--pg4 a)
saldo :: Extracto -> Double
saldo [(a,b,c,m)] = m
saldo ((d,t,dat,m):xs) | (t==Credito) = m+saldo xs
| otherwise = (-m)+saldo xs
cmpData::Data->Data->Bool
cmpData (D a m d) (D a1 m1 d1) | (a< a="="a1)" otherwise =" False"> Bool
porOrdem [a] = True
porOrdem ((x,y,dat,z):(x1,y1,dat1,z1):xs) = (cmpData dat dat1) && porOrdem ((x1,y1,dat1,z1):xs)
--pg4 c)
dmaxDebito :: Extracto -> Maybe (Data,Montante)
dmaxDebito ((_,Debito,d,m):t) = maxdeb (d,m) (dmaxDebito t)
dmaxDebito (h:t) = dmaxDebito t
dmaxDebito [] = Nothing
maxdeb:: (Data,Montante)->Maybe (Data,Montante)->Maybe (Data,Montante)
maxdeb v Nothing = Just v
maxdeb (d,m) (Just (d1,m1)) | (m > m1) = Just (d,m)
| otherwise = Just (d1,m1)
--Parte II
--pg1 a)
ordenaPorData::Extracto->Extracto
ordenaPorData [] = []
ordenaPorData ((d,t,dat,m):xs) = ordenaOneByOne (d,t,dat,m) (ordenaPorData xs)
ordenaOneByOne::Movimento->Extracto->Extracto
ordenaOneByOne x [] = [x]
ordenaOneByOne (d,t,dat,m) ((d1,t1,dat1,m1):xs) | (cmpData dat dat1) = ((d,t,dat,m):(d1,t1,dat1,m1):xs)
| otherwise = ((d1,t1,dat1,m1):(d,t,dat,m):xs)
relatorio :: Extracto -> String
relatorio l = let lord = ordenaPorData l
in
((" Data Descricao Credito Debito\n-----------------------------")++(printExt lord)++"--------------------\nSaldo: "++show (saldo lord))
where
printExt:: Extracto -> String
printExt [] = "\n"
printExt ((d,t,(D a mes dia),m):xs) | (t==Credito) = show a++"-"++show mes++"-"++show dia++"\t"++show d ++ "\t" ++ show m ++ "\t\n" ++ printExt xs
| otherwise =show a++"-"++show mes++"-"++show dia++"\t"++show d ++ "\t" ++ show m ++ "\t\n" ++ printExt xs
--pg2
proximo::Integer->Integer
proximo n = let strn = show n
in
read(nextNum strn 1)::Integer
where
nextNum::String->Int->String
nextNum [x] n= [head(show n)]++[x]
nextNum (a:b:xs) n | (a == b) = nextNum (b:xs) (n+1)
| otherwise = [head(show n)]++[a]++(nextNum (b:xs) 1)
anterior::Integer->Integer
anterior n = let strn = show n
in
if length strn > 1 then
(read (antNum (tuples strn))::Integer)
else
0
where
antNum::[(Integer,Char)]->String
antNum [] = ""
antNum ((n,c):xs) =(nNums (n,c)) ++ antNum xs
nNums::(Integer,Char)->String
nNums (0, c) = ""
nNums (n,c) = c:(nNums ((n-1),c))
tuples::String->[(Integer,Char)]
tuples [] = []
tuples (a:b:xs) = (read [a]::Integer,b):tuples xs
module Exame where
data Tree a = Empty | Node a (Tree a) (Tree a)
type Extracto = [Movimento]
type Movimento = (Descricao, Tipo, Data, Montante)
type Descricao = String
data Tipo = Credito | Debito deriving (Eq,Show)
type Ano = Int
type Mes = Int
type Dia = Int
data Data = D Ano Mes Dia
deriving (Eq,Ord,Show)
type Montante = Double -- valor positivo
-- Dados para teste
movimento1=("Compra", Debito, (D 1999 12 01), 123.0)
movimento2=("EuroMilhoes", Credito, (D 2009 08 23), 155.5)
movimento3=("EDP", Debito, (D 2009 01 26), 32.0)
extracto = [movimento1,movimento2,movimento3]
-------------------------------
--pg1
menoresMaiores :: Float -> Tree Float -> ([Float],[Float])
menoresMaiores n Empty = ([],[])
menoresMaiores n arv = let l = toList arv
in
((filter (n>) l),(filter (n<) l)) where toList Empty = [] toList (Node n esq dir) = [n] ++ toList esq ++ toList dir --pg2 ePrefixo :: String -> String -> Bool
ePrefixo [] r = True
ePrefixo l [] = True
ePrefixo (x:xs) (y:ys) | (x==y)= True && ePrefixo xs ys
| otherwise = False
--pg3
procura :: Descricao -> Extracto -> [(Tipo,Data,Montante)]
procura des ext = map (\(x,y,w,z)->(y,w,z)) (filter (\(x,y,w,z)->(des==x)) ext)
--pg4 a)
saldo :: Extracto -> Double
saldo [(a,b,c,m)] = m
saldo ((d,t,dat,m):xs) | (t==Credito) = m+saldo xs
| otherwise = (-m)+saldo xs
cmpData::Data->Data->Bool
cmpData (D a m d) (D a1 m1 d1) | (a
porOrdem [a] = True
porOrdem ((x,y,dat,z):(x1,y1,dat1,z1):xs) = (cmpData dat dat1) && porOrdem ((x1,y1,dat1,z1):xs)
--pg4 c)
dmaxDebito :: Extracto -> Maybe (Data,Montante)
dmaxDebito ((_,Debito,d,m):t) = maxdeb (d,m) (dmaxDebito t)
dmaxDebito (h:t) = dmaxDebito t
dmaxDebito [] = Nothing
maxdeb:: (Data,Montante)->Maybe (Data,Montante)->Maybe (Data,Montante)
maxdeb v Nothing = Just v
maxdeb (d,m) (Just (d1,m1)) | (m > m1) = Just (d,m)
| otherwise = Just (d1,m1)
--Parte II
--pg1 a)
ordenaPorData::Extracto->Extracto
ordenaPorData [] = []
ordenaPorData ((d,t,dat,m):xs) = ordenaOneByOne (d,t,dat,m) (ordenaPorData xs)
ordenaOneByOne::Movimento->Extracto->Extracto
ordenaOneByOne x [] = [x]
ordenaOneByOne (d,t,dat,m) ((d1,t1,dat1,m1):xs) | (cmpData dat dat1) = ((d,t,dat,m):(d1,t1,dat1,m1):xs)
| otherwise = ((d1,t1,dat1,m1):(d,t,dat,m):xs)
relatorio :: Extracto -> String
relatorio l = let lord = ordenaPorData l
in
((" Data Descricao Credito Debito\n-----------------------------")++(printExt lord)++"--------------------\nSaldo: "++show (saldo lord))
where
printExt:: Extracto -> String
printExt [] = "\n"
printExt ((d,t,(D a mes dia),m):xs) | (t==Credito) = show a++"-"++show mes++"-"++show dia++"\t"++show d ++ "\t" ++ show m ++ "\t\n" ++ printExt xs
| otherwise =show a++"-"++show mes++"-"++show dia++"\t"++show d ++ "\t" ++ show m ++ "\t\n" ++ printExt xs
--pg2
proximo::Integer->Integer
proximo n = let strn = show n
in
read(nextNum strn 1)::Integer
where
nextNum::String->Int->String
nextNum [x] n= [head(show n)]++[x]
nextNum (a:b:xs) n | (a == b) = nextNum (b:xs) (n+1)
| otherwise = [head(show n)]++[a]++(nextNum (b:xs) 1)
anterior::Integer->Integer
anterior n = let strn = show n
in
if length strn > 1 then
(read (antNum (tuples strn))::Integer)
else
0
where
antNum::[(Integer,Char)]->String
antNum [] = ""
antNum ((n,c):xs) =(nNums (n,c)) ++ antNum xs
nNums::(Integer,Char)->String
nNums (0, c) = ""
nNums (n,c) = c:(nNums ((n-1),c))
tuples::String->[(Integer,Char)]
tuples [] = []
tuples (a:b:xs) = (read [a]::Integer,b):tuples xs
Comentários