-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADTLecture.hs
More file actions
126 lines (100 loc) · 4.01 KB
/
ADTLecture.hs
File metadata and controls
126 lines (100 loc) · 4.01 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module Revision where
--Algebraic data types
--basic data types (Bool, Char, Float..etc)
--example of defining data types and values they can take
-- data Bool = True | False
--All data types and synonyms must start with a capital letter
--data type for days of week
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
deriving Eq
--function to use this data type
isWeekend :: Day -> Bool
isWeekend Sat = True
isWeekend Sun = True
isWeekend _ = False
--for this to work we need to derive Eq so that we can use the '==' operator on types of day
isWeekend' :: Day -> Bool
isWeekend' day
| day == Sat = True
| day == Sun = True
| otherwise = False
isWorkDay = not . isWeekend --returns opposite of isWeekend, using the 'not' function and dot notation
isPayDay :: Day -> Bool
isPayDay Fri = True
isPayDay _ = False
-----------------------------------------------------------------
type LCardNum = (Int, Int, Int, Int)
type AmCardNum = (Int, Int, Int)
type Date = (Int, Int)
type Since = Int
data PaymentCard = Visa LCardNum Date --LCardNum and Date are just info that this data value carries
| Mastercard LCardNum Date
| Amex AmCardNum Date Since
--here are some predefined cards/some variables of the 'PaymentCard' data type
myCard = Amex (4444, 333333, 11111) (8, 15) 79
deptCard = Visa (5555, 6666, 2222, 1111) (12, 14)
isAcceptedCard :: Date -> PaymentCard -> Bool
isAcceptedCard currentDate (Visa num exp) = isWithinExpiry exp currentDate
isAcceptedCard currentDate (Mastercard num exp) = isWithinExpiry exp currentDate
isAcceptedCard currentDate _ = False
-- isWithinExpiry should take an expiry date and the current date and check if the card has not yet expired
isWithinExpiry :: Date -> Date -> Bool
isWithinExpiry (exp_month, exp_year) (curr_month, curr_year)
| exp_year<curr_year = False
| exp_year>curr_year = True
| otherwise = if exp_month>curr_month
then True
else False
data Shape = Square Side
| Rectangle Width Length
| Circle Radius
type Side = Double
type Width = Double
type Length = Double
type Radius = Double
--list of some shapes
myShape = Rectangle 4.3 5.8
coolShape = Circle 6.2
basicShape = Square 1
isCircle :: Shape -> Bool
isCircle (Circle r) = True
isCircle _ = False
--can use '_' instead of 'r' because 'r' isnt used on right side of equation
isCircle' :: Shape -> Bool
isCircle' (Circle _) = True
isCircle' _ = False
isRed :: Suit -> Bool
idRed Hearts = True
isRed Diamonds = True
isRed _ = False
--(Eq allows us to use '==' and '/=' operators)
isRed' suit =
suit == Diamonds || suit == Hearts
----------------------------------------------------------------------------------------------
--type-class is a collection of types that share common operands.
--all basic types are members of the Eq type class.
--Example type class': Ord, Eq, Num, Show
--if we are using (our own) algebraic data types we have to derive a type-class
--to use its operands.
data Suit = Clubs | Diamonds | Hearts | Spades deriving (Eq, Ord, Show)
data Value = The Int | Jack | Queen | King | Ace deriving (Eq, Ord, Show)
data PlayingCard = Card Value Suit deriving (Eq, Ord, Show)
--when Ord is derived the greatest value is on the right side
--so for the 'value' data type 'The' is Lesser than 'Queen'
--and 'Jack' is Lesser than 'King'
--so Jack>King would return False
--cards
card1 = Card (The 7) Diamonds
card2 = Card (Ace) Hearts
bullet = Card Ace Spades
printCard :: PlayingCard -> String
printCard (Card val suit) =
if val < Jack
then (show val) ++ " of " ++ (show suit)
else "The " ++ (show val) ++ " of " ++ (show suit)
--Summary
--We used the keyword data to make new types.
--Types constructed in this way are known as Algebraic Data Types
--The new type has a name
--The new type has data constructors which are named
--The data constructors can take arguments