Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data Month = MakeMonth String deriving (Eq, Ord)
- toMonth :: String -> Month
- toMonth x
- | (read x :: Integer) < 1 = error "Minimum month number is 1"
- | (read x :: Integer) > 12 = error "Maximum month number is 12"
- | otherwise = MakeMonth x
- fromMonth :: Month -> String
- fromMonth (MakeMonth i) = i
- data Day = MakeDay String deriving (Eq, Ord)
- toDay :: String -> Day
- toDay x
- | (read x :: Integer) < 1 = error "Minimum day number is 1"
- | (read x :: Integer) > 31 = error "Maximum day number is 31"
- | otherwise = MakeDay x
- fromDay :: Day -> String
- fromDay (MakeDay i) = i
- newtype Year = MakeYear String deriving (Eq, Ord)
- toYear :: String -> Year
- toYear x
- | x == "0" = error "No year 0"
- | otherwise = MakeYear x
- fromYear :: Year -> String
- fromYear (MakeYear x) = x
- data Date = Date { year :: Year, month :: Month, day :: Day } deriving(Eq, Ord)
- leapYear y
- | mod y 400 == 0 = True
- | mod y 100 == 0 = False
- | mod y 4 == 0 = True
- | otherwise = False
- correctDate :: Integer -> Integer -> Integer -> Bool
- correctDate 0 _ _ = False
- correctDate y m d
- | (elem m [1,3,5,7,8,10,12]) && (elem d [1..31]) = True
- | (elem m [4,6,9,11]) && (elem d [1..30]) = True
- | (m==2) && (elem d [1..28]) = True
- | (leapYear y) && (m==2) && (d==29) = True
- | otherwise = False
- makeDate :: String -> String -> String -> Date
- makeDate y m d
- | correctDate yInt mInt dInt = Date { year = toYear y, month = toMonth m, day = toDay d }
- | otherwise = error "Bad date"
- where yInt = read y :: Integer
- mInt = read m :: Integer
- dInt = read d :: Integer
- data EventInfo = EventInfo { name :: String
- , place :: String
- , date :: Date
- } deriving(Eq)
- -- findEventByPlace :: String -> [EventInfo] -> [EventInfo]
- -- findEventByPlace plc events = filter (\p -> place p == plc) events
- -- findEventsByDate :: String -> [EventInfo] -> [EventInfo]
- -- findEventsByDate dt events = filter (\d -> date d == dt) events
- -- aboutEvent :: String -> [EventInfo] -> EventInfo
- doCommand :: String -> IO [EventInfo] -> IO ()
- doCommand input ioEvents = do
- events <- ioEvents -- Now you can use events as [EventInfo]
- possiblyChangedEvents <- checkCommand input events
- loop $ return [possiblyChangedEvents]
- errorMsg = "I do not understand that. I understand the following: \n\
- \*Event <name> happens at <place> on <date> \n\
- \*Tell me about <eventname> \n\
- \*What happens on <date> \n\
- \*What happens at <place> \n\
- \*Quit"
- checkCommand :: String -> [EventInfo] -> [EventInfo]
- checkCommand input events
- | concat cmd == "Eventhappensaton" = events
- | concat cmd == "Tellmeabout" = events
- | concat cmd == "Whathappenson" = events
- | concat cmd == "Whathappensat" = events
- | otherwise = error errorMsg
- where cmd = filter (\n -> head n /= '\'' && last n /= '\'') $ words input
- info = filter (\n -> head n == '\'' || last n == '\'') $ words input
- main = loop $ return []
- loop :: IO [EventInfo] -> IO ()
- loop ioEvents = do
- input <- getLine
- if input == "Quit"
- then putStrLn "bye"
- else doCommand input ioEvents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement