-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter_10_my_note.hs
407 lines (309 loc) · 12.9 KB
/
Chapter_10_my_note.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
module Chapter_10_my_note where
import Prelude hiding (unzip, last, init, getLine)
import Test.QuickCheck
import Test.QuickCheck.Function
doubleAllv1 :: [Integer] -> [Integer]
doubleAllv1 ls = [x * 2 | x <- ls]
doubleAllv2 :: [Integer] -> [Integer]
doubleAllv2 [] = []
doubleAllv2 (x : xs) = (2 * x) : doubleAllv2 xs
doubleAllv3 :: [Integer] -> [Integer]
doubleAllv3 = map (\x -> 2 * x)
lengthMapSum :: [a] -> Int
lengthMapSum = sum . map (\x -> 1)
addUpv1 :: [Integer] -> [Integer]
addUpv1 ns = filter (> 1) (map (+ 1) ns)
addUpv2 :: [Integer] -> [Integer]
addUpv2 ns = map (+ 1) (filter (> 0) ns)
squareList :: [Integer] -> [Integer]
squareList = map (\x -> x * x)
squareListSum :: [Integer] -> Integer
squareListSum = sum . map (\x -> x * x)
allGreaterZero :: [Integer] -> Bool
allGreaterZero = and . map (> 0)
minFunScope :: (Ord t, Num t) => (t -> t) -> [t] -> t
minFunScope func ls = minimum (map func ls)
allFunEqual :: (Ord t, Num t) => (t -> t) -> [t] -> Bool
allFunEqual func ls = null (filter (/= func (head ls)) (map func ls))
allGreaterFunZero :: (Ord t, Num t) => (t -> t) -> [t] -> Bool
allGreaterFunZero func ls = and (map ((> 0) . func) ls)
funIssorted :: (Ord t, Num t) => (t -> t) -> [t] -> Bool
funIssorted func ls = isSorted (map func ls)
isSorted :: (Ord t, Num t) => [t] -> Bool
isSorted [] = True
isSorted [_] = True
isSorted (x : y : xs) = x < y && isSorted (y : xs)
twice :: (t -> t) -> t -> t
twice func = func . func
iter :: Integer -> (t -> t) -> t -> t
iter times func val
| times < 0 = error "wrong time data given"
| times == 0 = val
| otherwise = iter (times - 1) func (func val)
base2 :: Integer -> Integer
base2 n
| n >= 0 = iter n (\x -> 2 * x) 1
| otherwise = error "time negative"
propFilter :: Fun Integer Bool -> [Integer] -> Bool
propFilter func ls = filter (apply func) ls == filter (apply func) (filter (apply func) ls)
propFilterTestEnv :: (Fun Integer Bool -> [Integer] -> Bool) -> IO ()
propFilterTestEnv = quickCheck
rev :: [a] -> [a]
rev = foldr snoc []
snoc :: a -> [a] -> [a]
snoc x xs = xs ++ [x]
toNSquareSum :: Integer -> Integer
toNSquareSum val
| val > 0 = foldr (+) 0 (map (\x -> x * x) [1 .. val])
| otherwise = error "scope error"
listSquareSum :: [Integer] -> Integer
listSquareSum ls = foldr (+) 0 (map (\x -> x * x) ls)
unzip :: [(a, b)] -> ([a], [b])
unzip orig = foldr fragUnzip ([], []) orig
fragUnzip :: (a, b) -> ([a], [b]) -> ([a], [b])
fragUnzip (p1, p2) (w1, w2) = (p1 : w1, p2 : w2)
last1 :: [a] -> a
last1 = foldr1 (\_ x -> x)
last :: [a] -> a
last ls
| not $ null ls = head $ foldr (\x y -> if null y then [x] else y) [] ls
| otherwise = error "length 0"
init :: [a] -> [a]
init ls
| null ls = error "length 0"
| otherwise = tail (foldr tailToHead [] ls)
where
tailToHead :: a -> [a] -> [a]
tailToHead frag lst = if null lst then [frag] else [head lst] ++ [frag] ++ tail lst
type Line = String
formatLine :: Line -> String
formatLine xs = xs
formatList :: (a -> String) -> [a] -> String
formatList formatItem ls = foldr (++) [] (map formatItem ls)
formatLines :: [Line] -> String
formatLines = formatList formatLine
filterFirst :: (a -> Bool) -> [a] -> [a]
filterFirst _ [] = []
filterFirst filtCond ls = if filtCond (head ls)
then tail ls
else head ls : filterFirst filtCond (tail ls)
-- returnLoan use filterFirst or all the book will come out...
filterLast :: (a -> Bool) -> [a] -> [a]
filterLast _ [] = []
filterLast filtCond ls = if filtCond (last ls)
then init ls
else filterLast filtCond (init ls) ++ [last ls]
filterLast1 :: (a -> Bool) -> [a] -> [a]
filterLast1 filtCond ls = reverse (filterFirst filtCond (reverse ls))
switchMap :: (a -> b) -> (a -> b) -> [a] -> [b]
switchMap _ _ [] = []
switchMap fun1 _ [x] = [fun1 x]
switchMap fun1 fun2 (x1 : x2 : rest) = fun1 x1 : fun2 x2 : switchMap fun1 fun2 rest
split :: [a] -> ([a], [a])
split [] = ([], [])
split [x] = ([x], [])
split (x1 : x2 : rest) = (x1 : fst (split rest), x2 : snd (split rest))
merge :: ([a], [a]) -> [a]
merge (x : xs, y : ys) = x : y : merge (xs, ys)
merge (x : _, []) = [x]
merge (_, _) = []
-- g (foldr1 g xs) (foldr1 g ys) == foldr1 g (xs ++ ys)
dropUntil :: (a -> Bool) -> [a] -> [a]
dropUntil _ [] = []
dropUntil cond (l : ls) = if cond l
then l : ls
else dropUntil cond ls
isSpace :: Char -> Bool
isSpace ch = ch `elem` [' ', '\n', '\r', '\t']
dropSpace :: String -> String
dropSpace = dropUntil (not . isSpace)
dropWord :: String -> String
dropWord = dropUntil isSpace
getUntil :: (a -> Bool) -> [a] -> [a]
getUntil _ [] = []
getUntil f (x : xs) = if f x
then []
else x : getUntil f xs
getWord :: String -> String
getWord line = getUntil isSpace (dropSpace line)
dropLine :: String -> String
dropLine = dropSpace . dropWord . dropSpace
strToLine :: String -> [String]
strToLine [] = []
strToLine line = getWord line : strToLine (dropLine line)
getLine :: Int -> (a -> Int) -> [a] -> [a]
getLine _ _ [] = []
getLine len f (x : xs)
| f x <= len = x : getLine (len - f x - 1) f xs
| otherwise = []
multiDropLine :: Int -> (a -> Int) -> [a] -> [a]
multiDropLine _ _ [] = []
multiDropLine len f (x : xs) = if len >= f x
then multiDropLine (len - f x - 1) f xs
else x : xs
multiSplitLines :: Int -> (a -> Int) -> [a] -> [[a]]
multiSplitLines _ _ [] = []
multiSplitLines len f (x : xs) = getLine len f (x : xs)
: multiSplitLines len f (multiDropLine len f (x : xs))
type Picture = [String]
invertColorDot :: Char -> Char
invertColorDot ch = if ch == '#' then '.' else '#'
invertColorLine :: String -> String
invertColorLine = map invertColorDot
invertColor :: Picture -> Picture
invertColor = map invertColorLine
data PicSize = ReguPic Int Int |
IrrePic Int
deriving (Eq, Show)
isReg :: Picture -> Bool
isReg pic = and [length line == length (head pic) | line <- pic]
picSizeGen :: Picture -> PicSize
picSizeGen pic = if isReg pic
then ReguPic (length pic) (length (head pic))
else IrrePic (length pic)
-- picSizeComp :: Picture -> Picture -> Bool
-- picSizeComp p1 p2 = if isReg p1 == isReg p2
-- then picSizeGen p1 == picSizeGen p2
-- else error "there is a regular pic and a irregular one"
-- superimpose :: Picture -> Picture -> Picture
-- superimpose = undefined
isSizeReg :: PicSize -> Bool
isSizeReg (ReguPic _ _) = True
isSizeReg (IrrePic _) = False
{-
compariation of figures:
-----> not same regularity: ------> False now temporary
|
---> same regularity: ------> all regular ----> same size DONE
| |
| ---> not same size DONE
-----> all irregular
-}
figRegComp :: PicSize -> PicSize -> Bool
figRegComp size1 size2 = isSizeReg size1 == isSizeReg size2
&& isSizeReg size1
colorPriority :: Char -> Char -> Char
colorPriority ch1 ch2 = if ch1 == '#' || ch2 == '#'
then '#'
else '.'
superimpose :: Picture -> Picture -> Picture
superimpose p1 p2 = if figRegComp (picSizeGen p1) (picSizeGen p2)
then if picSizeGen p1 == picSizeGen p2
then map zipColor (zip p1 p2)
else mergePics p1 p2
else error "feature not supported... sorry!"
zipColor :: (String, String) -> String
zipColor (l1, l2) = zipWith colorPriority l1 l2
_regPicSize :: PicSize -> (Int, Int)
_regPicSize (ReguPic a b) = (a, b)
_regPicSize _ = error "regpicsize error"
regPicSize :: Picture -> (Int, Int)
regPicSize = _regPicSize . picSizeGen
largestSize :: Picture -> Picture -> (Int, Int)
largestSize p1 p2 = (max (fst $ regPicSize p1) (fst $ regPicSize p2),
max (snd $ regPicSize p1) (fst $ regPicSize p2))
blankLineGen :: Int -> String
blankLineGen val = concat ["." | _ <- [1 .. val]]
changeSizeTo :: Picture -> (Int, Int) -> Picture -- first int is width, second is height
changeSizeTo pic (width, height) =
[line ++ blankLineGen (width - length (head pic)) | line <- pic]
++ [blankLineGen width | _ <- [1 .. height - length pic]]
changeToLargest :: Picture -> Picture -> Picture -> Picture
changeToLargest base cp1 cp2 = changeSizeTo base (largestSize cp1 cp2)
mergePics :: Picture -> Picture -> Picture
mergePics p1 p2 = map zipColor
(zip (changeToLargest p1 p1 p2) (changeToLargest p2 p1 p2))
getVert :: Int -> String -> String -> String
getVert val str remi = str !! val : remi
rotate901 :: Picture -> Picture
rotate901 pic = [foldr (getVert i) [] pic | i <- [0 .. length (head pic) - 1]]
rotate90 :: Picture -> Picture
rotate90 [] = []
rotate90 pic = reverse (map head pic) : reverse (map tail pic)
type Person = String
type Book = String
type Database = [(Person, Book)]
books :: Database -> Person -> [Book]
books db name = (fst . unzip) (filter (matchName name) db)
where
matchName :: Person -> (Person, Book) -> Bool
matchName nm pt = fst pt == nm
borrowers :: Database -> Book -> [Person]
borrowers db name = (snd . unzip) (filter (matchName name) db)
where
matchName :: Book -> (Person, Book) -> Bool
matchName nm pt = snd pt == nm
isBorrowed :: Database -> Book -> Bool
isBorrowed db name = name `elem` (snd . unzip) db
numBorrowed :: Database -> Book -> Int
numBorrowed db name = length (filter (matchName name) db)
where
matchName :: Book -> (Person, Book) -> Bool
matchName nm pt = snd pt == nm
makeLoan :: Database -> Person -> Book -> Database
makeLoan db pname bname = (pname, bname) : db
returnLoan :: Database -> Person -> Book -> Database
returnLoan db pname bname = filterFirst (matchName (pname, bname)) db
where
matchName :: (Person, Book) -> (Person, Book) -> Bool
matchName test part = fst test == fst part && snd test == snd part
type DataBase = [(Person, [Book])]
_books :: DataBase -> Person -> [Book]
_books db name = (snd . head) (filter (matchName name) db)
where
matchName :: Person -> (Person, [Book]) -> Bool
matchName nm part = fst part == nm
_borrowers :: DataBase -> Book -> [Person]
_borrowers db name = (fst . unzip) (filter (matchName name) db)
where
matchName :: Book -> (Person, [Book]) -> Bool
matchName bk pr = bk `elem` snd pr
_borrowed :: DataBase -> Book -> Bool
_borrowed db name = (not . null) (filter (matchName name) db)
where
matchName :: Book -> (Person, [Book]) -> Bool
matchName bk pr = bk `elem` snd pr
_numBorrowed :: DataBase -> Person -> Int
_numBorrowed db name = (length . snd . unzip) (filter (matchName name) db)
where
matchName :: Person -> (Person, [Book]) -> Bool
matchName nm pr = nm == fst pr
_makeLoan :: DataBase -> Person -> Book -> DataBase
_makeLoan db pname bname = map (insertBook pname bname) db
where
insertBook :: Person -> Book -> (Person, [Book]) -> (Person, [Book])
insertBook pnm bnm pr = if fst pr == pnm
then (pnm, bnm : snd pr)
else pr
_returnLoan :: DataBase -> Person -> Book -> DataBase
_returnLoan db pname bname = map (filterOneBook pname bname) db
where
filterOneBook :: Person -> Book -> (Person, [Book]) -> (Person, [Book])
filterOneBook pnm bnm pr = if pnm == fst pr
then (pnm, filterFirst (== bnm) (snd pr))
else pr
data Move = Rock | Paper | Scissors
deriving (Eq)
instance Show Move where
show Rock = "r"
show Paper = "p"
show Scissors = "s"
convertMove :: Char -> Move
convertMove 'r' = Rock
convertMove 'R' = Rock
convertMove 'p' = Paper
convertMove 'P' = Paper
convertMove 's' = Scissors
convertMove 'S' = Scissors
convertMove _ = error "do not match"
type Tournament = ([Move], [Move])
outcome :: Move -> Move -> Integer
outcome Rock Paper = -1
outcome Paper Rock = 1
outcome Paper Scissors = -1
outcome Rock Scissors = 1
outcome Scissors Paper = 1
outcome Scissors Rock = -1
outcome _ _ = 0
tournamentOutcome :: Tournament -> Integer
tournamentOutcome tour = sum (zipWith outcome (fst tour) (snd tour))