-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17_records.hs
executable file
·51 lines (41 loc) · 1.04 KB
/
17_records.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
43
44
45
46
47
48
49
50
51
#!/usr/bin/env runhaskell
type Year = Int
type Month = Int
type Day = Int
-- Introducing records. They are the same as before, but they automatically
-- create new functions:
-- year :: Date -> Year
-- month :: Date -> Month
-- day :: Date -> Day
-- Date: YYYY MMM D
data Date = Date {
year :: Year,
month :: Month,
day :: Day
}
deriving (Show)
tomorrow = Date 2012 11 17
display :: Date -> String
display d = naturalMonth (month d) ++ " " ++ naturalDay (day d) ++ ", " ++ (show $ year d)
main :: IO ()
main = do
putStrLn $ display $ tomorrow
naturalDay :: Day -> String
naturalDay 1 = "1st"
naturalDay 2 = "2nd"
naturalDay 3 = "3rd"
naturalDay n = (show n) ++ "th"
naturalMonth :: Month -> String
naturalMonth 1 = "January"
naturalMonth 2 = "February"
naturalMonth 3 = "March"
naturalMonth 4 = "April"
naturalMonth 5 = "May"
naturalMonth 6 = "June"
naturalMonth 7 = "July"
naturalMonth 8 = "August"
naturalMonth 9 = "September"
naturalMonth 10 = "October"
naturalMonth 11 = "November"
naturalMonth 12 = "December"
naturalMonth _ = "Other" -- ??