Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Function
- type Name = String
- type Rate = Integer
- data Transaction = CreditOut Integer | CreditIn Integer
- deriving (Eq, Show)
- type Customer = (Name, [Transaction])
- type DB = [Customer]
- testDB :: DB
- testDB = [
- ("Alma", [CreditIn 3000, CreditOut 200, CreditOut 1500]),
- ("Anna", [CreditIn 5000, CreditOut 5100]),
- ("Feri", [CreditOut 1000, CreditIn 200, CreditIn 200, CreditOut 10])
- ]
- testDB2 :: DB
- testDB2 = [
- ("Ani", [CreditIn 3000]),
- ("Juli", [CreditOut 1000, CreditIn 200])
- ]
- ---
- emptyDB::DB
- emptyDB = []
- --Benne van-e az adott ügyfél az adatbázisban?
- isCust nev db = any ((==nev).fst) db
- add::Name -> Integer -> DB ->DB
- add nev qty db
- | (isCust nev db) && (qty>=0) = updateP nev qty db
- | (isCust nev db) && (qty<0) = updateN nev qty db
- | otherwise = db
- --updateP: növeli az egyenleget
- updateP _ _ [] = []
- updateP nev qty (entry:db)
- | fst entry == nev = ((fst entry), (CreditIn qty):(snd entry)):db
- | otherwise = entry:(updateP nev qty db)
- --updateN: csökkenti az egyenleget
- updateN _ _ [] = []
- updateN nev qty (entry:db)
- | fst entry == nev = ((fst entry), (CreditOut (abs qty)):(snd entry)):db
- | otherwise = entry:(updateN nev qty db)
- ---
- sumTrans [] = 0
- sumTrans ((CreditIn t):ts) = t+(sumTrans ts)
- sumTrans ((CreditOut t):ts) = (sumTrans ts)-t
- summarize :: DB ->[(Name,Integer)]
- summarize db = map (\(a,t) -> (a,(sumTrans t))) db
- ---
- encloseDay :: Rate -> DB -> DB
- encloseDay _ [] = []
- encloseDay rate (entry:db)
- | (q >= 0) = (fst entry, [(CreditIn (rate*q))]):(encloseDay rate db)
- | otherwise = (fst entry, [(CreditOut (abs q))]):(encloseDay rate db)
- where q = (sumTrans (snd entry))
- ---
- comparing p a b = compare (p a) (p b)
- maxOut db = maximumBy (comparing (sumTrans.snd)) db
- maxIn db = minimumBy (comparing (sumTrans.snd)) db
- maximums :: DB -> (Name,Name,Integer)
- maximums db = ((fst (maxIn db)),(fst (maxOut db)),q)
- where q = (sumTrans (snd (maxOut db)))-(sumTrans (snd (maxIn db)))
- ---
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement