-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit2.hs
327 lines (246 loc) · 7.46 KB
/
unit2.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
-- lesson 11
half :: Int -> Double
half 0 = error "cant get the half of 0"
half n = fromIntegral n / 2
halve :: Integer -> Integer
halve n = n `div` 2
printDouble :: Int -> String
printDouble x = show (x * 2)
makeAddress :: Int -> (String -> String -> (Int, String, String))
makeAddress doorNo street town = (doorNo, street, town)
same :: a -> a
same x = x
safeTail :: [a] -> [a]
safeTail [] = []
safeTail (x : xs) = xs
-- lesson 12
type FirstName = String
type LastName = String
type MiddleName = String
showName :: Name -> String
showName (Name f l) = f ++ " " ++ l
showName (NameWithMiddle f m l) = f ++ " " ++ m ++ " " ++ l
data Name = Name FirstName LastName | NameWithMiddle FirstName MiddleName LastName
type Age = Int
type Height = Int
type Weight = Int
patientInfo :: PatientName -> Age -> Height -> String
patientInfo (fname, lname) age height = name ++ " " ++ ageHeight
where
name = lname ++ ", " ++ fname
ageHeight = "(" ++ show age ++ "yrs." ++ show height ++ "in." ++ ")"
type PatientName = (String, String)
firstName :: PatientName -> String
firstName = fst
lastName :: PatientName -> String
lastName = snd
data Sex = Male | Female
sexInitial :: Sex -> Char
sexInitial Male = 'M'
sexInitial Female = 'F'
data RhType = Pos | Neg
data ABOType = A | B | AB | O
data BloodType = BloodType ABOType RhType
showRh :: RhType -> String
showRh Pos = "+"
showRh Neg = "-"
showABO :: ABOType -> String
showABO A = "A"
showABO B = "B"
showABO AB = "AB"
showABO O = "O"
showBloodType :: BloodType -> String
showBloodType (BloodType abo rh) = showABO abo ++ showRh rh
canDonateTo :: BloodType -> BloodType -> Bool
canDonateTo (BloodType O _) _ = True -- Universal Donor
canDonateTo _ (BloodType AB _) = True -- Universal reciver
canDonateTo (BloodType A _) (BloodType A _) = True
canDonateTo (BloodType B _) (BloodType B _) = True
canDonateTo _ _ = False -- otherwise
-- data Patient = Patient Name Sex Age Height Weight BloodType
data Patient = Patient
{ name :: Name,
sex :: Sex,
age :: Int,
height :: Int,
weight :: Int,
bloodType :: BloodType
}
donorFor :: Patient -> Patient -> Bool
donorFor p1 p2 = canDonateTo (bloodType p1) (bloodType p2)
showSex :: Sex -> String
showSex Male = "Male"
showSex Female = "Female"
patientSummary :: Patient -> String
patientSummary p =
"********************\n"
++ "Patient: "
++ showName (name p)
++ "\n"
++ "Sex: "
++ showSex (sex p)
++ "\n"
++ "Age: "
++ show (age p)
++ "\n"
++ "Height: "
++ show (height p)
++ "\n"
++ "Weight: "
++ show (weight p)
++ "\n"
++ "Blood: "
++ showBloodType (bloodType p)
++ "\n"
++ "********************"
-- type Classes lesson: 13
myAdd :: (Num a) => a -> a -> a -- here Num is a type class
myAdd x y = x + y
class Discribable a where
describe :: a -> String
data IceCream = Chocolate | Venilla deriving (Show, Eq, Ord)
cycleSucc n = if n == maxBound
then minBound
else succ n
-- lesson 14
data SixSidedDie = S1 | S2 | S3 |S4 | S5 | S6 deriving (Eq , Ord, Enum, Bounded)
instance Show SixSidedDie where
show S1 = "I"
show S2 = "II"
show S3 = "III"
show S4 = "IV"
show S5 = "V"
show S6 = "VI"
-- this will cause error if u try [S1 ..]
-- instance Enum SixSidedDie where
-- toEnum 0 = S1
-- toEnum 1 = S2
-- toEnum 2 = S3
-- toEnum 3 = S4
-- toEnum 4 = S5
-- toEnum 5 = S6
-- toEnum _ = error "No such value"
-- fromEnum S1 = 0
-- fromEnum S2 = 1
-- fromEnum S3 = 2
-- fromEnum S4 = 3
-- fromEnum S5 = 4
-- fromEnum S6 = 5
newtype Name1 = Name1 (String, String) deriving (Eq,Show)
instance Ord Name1 where
compare (Name1 (f1 ,l1)) ( Name1 (f2,l2)) = compare (l2,f2) (l1,f1)
-- Excercise
data Number1 = One | Two | Three
instance Enum Number1 where
toEnum 1 = One
toEnum 2 = Two
toEnum 3 = Three
fromEnum One = 1
fromEnum Two = 2
fromEnum Three = 3
instance Eq Number1 where
(==) num1 num2 = fromEnum num1 == fromEnum num2
instance Ord Number1 where
compare num1 num2 = compare (fromEnum num1) (fromEnum num2)
data FiveSidedDie where
Side1 :: FiveSidedDie
Side2 :: FiveSidedDie
Side3 :: FiveSidedDie
Side4 :: FiveSidedDie
Side5 :: FiveSidedDie
deriving (Eq, Enum, Show)
class (Eq a, Enum a) => Die a where
roll :: Int -> a
instance Die FiveSidedDie where
roll n = toEnum (n `mod` 5)
-- lesson 15
data FourLetterAlphabet = L1 | L2 | L3 | L4 deriving ( Show , Bounded , Enum )
rotN :: (Enum a, Bounded a) => Int -> a -> a
rotN alphahabetSize c = toEnum rotation
where halfAlphabet = alphahabetSize `div` 2
offset = fromEnum c + halfAlphabet
rotation = offset `mod` alphahabetSize
fourLetterAlphabetEncoder :: [FourLetterAlphabet] -> [FourLetterAlphabet]
fourLetterAlphabetEncoder vals = map rot4l vals
where
alphaSize = 1 + fromEnum (maxBound :: FourLetterAlphabet)
rot4l = rotN alphaSize
rotNDecoder :: (Enum a, Bounded a) => Int -> a -> a
rotNDecoder n c = toEnum rotation
where
halfN = n `div` 2
offset = if even halfN
then fromEnum c + halfN
else 1 + fromEnum c + halfN
rotation = offset `mod` n
rotEncoder :: String -> String
rotEncoder text = map rotChar text
where
alphaSize = 1 + fromEnum (maxBound :: Char)
rotChar = rotN alphaSize
rotDecoder :: String -> String
rotDecoder text = map rotChar text
where
alphaSize = 1 + fromEnum (maxBound :: Char)
rotChar = rotNDecoder alphaSize
-- xor based encryption
xorBool :: Bool -> Bool -> Bool
xorBool val1 val2 = (val1 || val2) && not (val1 && val2)
xorPair :: (Bool,Bool) -> Bool
xorPair (val1,val2) = xorBool val1 val2
xor :: [Bool] -> [Bool] -> [Bool]
xor list1 list2 = map xorPair $ zip list1 list2
type Bits = [Bool]
intToBits' :: Int -> [Bool]
intToBits' 0 = [False]
intToBits' 1 = [True]
intToBits' n = if remainder == 0
then False : intToBits' nextVal
else True : intToBits' nextVal
where
remainder = n `mod` 2
nextVal = n `div` 2
maxBits :: Int
maxBits = length (intToBits' maxBound)
intToBits :: Int -> Bits
intToBits n = leadingFalses ++ reversedBits
where
reversedBits = reverse $ intToBits' n
missingBits = maxBits - length reversedBits
leadingFalses = take missingBits (cycle [False])
charToBits :: Char -> Bits
charToBits char = intToBits $ fromEnum char
bitsToInt :: Bits -> Int
bitsToInt bits = sum $ map (\x -> 2 ^ (snd x)) trueLocations
where size = length bits
indices = [size-1,size-2..0]
trueLocations = filter (\x -> fst x == True) (zip bits indices)
bitsToChar :: Bits -> Char
bitsToChar bits = toEnum (bitsToInt bits)
myPad :: String
myPad = "Shhhhhh"
myPlainText :: String
myPlainText = "Haskell"
applyOTP' :: String -> String -> [Bits]
applyOTP' pad plainText = map (\pair -> (fst pair) `xor` (snd pair)) ( zip padBits plainTextBits)
where
padBits = map charToBits pad
plainTextBits = map charToBits plainText
applyOTP :: String -> String -> String
applyOTP pad plainText = map bitsToChar bitsList
where bitsList = applyOTP' pad plainText
encoderDecoder :: String -> String
encoderDecoder = applyOTP myPad
class Chipher a where
encode :: a -> String -> String
decode :: a -> String -> String
data Rot = Rot
instance Chipher Rot where
encode Rot text = rotEncoder text
decode Rot text = rotDecoder text
newtype OneTimePad = OTP String
instance Chipher OneTimePad where
encode (OTP pad) text = applyOTP pad text
decode (OTP pad) text = applyOTP pad text
prng :: Int -> Int -> Int -> Int -> Int
prng a b maxVal seed = (a * seed + b) `mod` maxVal