-
Notifications
You must be signed in to change notification settings - Fork 98
/
expensereport.hs
42 lines (38 loc) · 1.37 KB
/
expensereport.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Control.Monad (forM_)
import Data.Array
import Data.IORef
data ExpenseType = Dinner | Breakfast | CarRental
data Expense = Expense { eType :: ExpenseType, amount :: Int }
printReport :: [Expense] -> IO ()
printReport expenses = do
total <- newIORef 0
--total :: IORef Integer
mealExpenses <- newIORef 0
--mealExpenses :: IORef Integer
putStrLn("Expense Report")
forM_ expenses $ \expense -> do
modifyIORef mealExpenses (+ (
case (eType expense) of
Dinner -> (amount expense)
Breakfast -> (amount expense)
_ -> 0
))
let expenseName = "" ++ case (eType expense) of
Dinner -> "Dinner"
Breakfast -> "Breakfast"
CarRental -> "Car Rental"
let mealOverExpensesMarker = case (eType expense) of
Dinner -> if (amount expense) > 5000 then "X" else " "
Breakfast -> if (amount expense) > 1000 then "X" else " "
_ -> " "
putStrLn(expenseName ++ "\t" ++ (show (amount expense)) ++ "\t" ++ mealOverExpensesMarker)
modifyIORef total (+ (amount expense))
putStr("Meal expenses: ")
readIORef mealExpenses >>= print
putStr("Total expenses: ")
readIORef total >>= print
main :: IO ()
main = do
printReport([
Expense Breakfast 50
])