-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter_6_my_note.hs
342 lines (269 loc) · 11.8 KB
/
Chapter_6_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
module Chapter_6_my_note where
import Prelude hiding (fst, id, snd, lookup)
import Test.QuickCheck hiding (scale, collect)
horse :: [String]
horse = [".......##...",
".....##..#..",
"...##.....#.",
"..#.......#.",
"..#...#...#.",
"..#...###.#.",
".#....#..##.",
"..#...#.....",
"...#...#....",
"....#..#....",
".....#.#....",
"......##...."]
id :: a -> a
id x = x
fst :: (a, b) -> a
fst (x, _) = x
snd :: (a, b) -> b
snd (_, y) = y
mystery :: (Bool, a) -> Char
mystery (x, _) = if x
then 'c'
else 'd'
sing :: a -> [a]
sing x = [x]
shift :: ((a, b), c) -> (a, (b, c))
shift ((x, y), z) = (x, (y, z))
type Picture = [String]
flipH :: Picture -> Picture
flipH = reverse
above :: Picture -> Picture -> Picture
above = (++)
flipV :: Picture -> Picture
flipV _pic = [reverse line | line <- _pic]
beside :: Picture -> Picture -> Picture
beside pic1 pic2 = [line1 ++ line2 | (line1, line2) <- zip pic1 pic2]
invertChar :: Char -> Char
invertChar ch = if ch == '.'
then '#'
else '.'
invertLine :: String -> String
invertLine line = [invertChar ch | ch <- line]
invertColour :: Picture -> Picture
invertColour pic = [invertLine line | line <- pic]
invertColour' :: Picture -> Picture
invertColour' pic = [[invertChar ch | ch <- line] | line <- pic]
prop_AboveFlipV :: Picture -> Picture -> Bool
prop_AboveFlipV pic1 pic2 = flipV (above pic1 pic2) == above (flipV pic1) (flipV pic2)
tester1 :: IO ()
tester1 = quickCheck prop_AboveFlipV
prop_AboveFlipH :: Picture -> Picture -> Bool
prop_AboveFlipH pic1 pic2 = flipH (above pic1 pic2) == above (flipH pic2) (flipH pic1)
tester2 :: IO ()
tester2 = quickCheck prop_AboveFlipH
superimposeChar :: Char -> Char -> Char
superimposeChar ch1 ch2 = if ch1 == ch2 && ch1 == '.'
then '.'
else '#'
superimposeString :: String -> String -> String
superimposeString line1 line2 = [superimposeChar (fst _pair) (snd _pair) | _pair <- zip line1 line2]
superimpose :: Picture -> Picture -> Picture
superimpose pic1 pic2 = [superimposeString (fst _p) (snd _p) | _p <- zip pic1 pic2]
printPicture :: Picture -> IO ()
printPicture pic = putStr (concat [line ++ "\n" | line <- pic])
rotatePos90 :: Picture -> Picture
rotatePos90 pic = flipV [concat [[line !! i] | line <- pic] | i <- [0 .. length (head pic) - 1]]
rotateNeg90 :: Picture -> Picture
rotateNeg90 pic = [concat [[line !! i] | line <- pic] | i <- [length (head pic) - 1, length (head pic) - 2 .. 0]]
scale :: Picture -> Integer -> Picture
scale _pic _num
| _num <= 0 = [""]
| otherwise = rotateNeg90 (orig (rotatePos90 (orig _pic _num)) _num)
where
orig :: Picture -> Integer -> Picture
orig pic num = [concat [concat [[ch] | _ <- [1 .. num]] | ch <- line] | line <- pic]
prop_BesideFlipV :: Picture -> Picture -> Bool
prop_BesideFlipV pic1 pic2 = flipV (beside pic1 pic2) == beside (flipV pic2) (flipV pic1)
tester3 :: IO ()
tester3 = quickCheck prop_BesideFlipV
prop_BesideFlipH :: Picture -> Picture -> Bool
prop_BesideFlipH pic1 pic2
| length pic1 == length pic2 = flipH (beside pic1 pic2) == beside (flipH pic1) (flipH pic2)
| otherwise = True
tester4 :: IO ()
tester4 = quickCheck prop_BesideFlipH
isRectangular :: Picture -> Bool
isRectangular pic = False `notElem` [maxLen pic == length line | line <- pic]
where
maxLen :: Picture -> Int
maxLen _pic = maximum [length line | line <- _pic]
fixToRectangular :: Picture -> Picture
fixToRectangular pic = if isRectangular pic
then pic
else [line ++ concat ["." | _ <- [0 .. maxLen pic - length line - 1]] | line <- pic]
where
maxLen :: Picture -> Int
maxLen _pic = maximum [length line | line <- _pic]
type Position = (Int, Int)
type Image = (Picture, Position)
makeImage :: Picture -> Position -> Image
makeImage pic pos = (pic, pos)
changePosition :: Image -> Position -> Image
changePosition (pic, _) _pos = (pic, _pos)
printImage :: Image -> IO ()
printImage (pic, (_x, _y))
| _x < 0 || _y < 0 = putStr "position is not in the first part.\n"
| otherwise = printPicture ([concat ["x" | _ <- [0 .. _x - 1]] ++ line | line <- pic]
++ [concat ["x" | _ <- [0 .. _x + maxLen pic - 1]] | _ <- [0 .. _y - 1]])
where
maxLen :: Picture -> Int
maxLen _pic = maximum [length line | line <- _pic]
aboveImg :: Image -> Image -> Image
aboveImg (pic1, (_x1, _y1)) (pic2, (_x2, _y2)) = (above pic1 pic2, (_x1, _y1))
type Name = String
type Price = Int
type BarCode = Int
type Database = [(BarCode, Name, Price)]
codeIndex :: Database
codeIndex = [ (4719, "Fish Fingers", 121)
, (5643, "Nappies", 1010)
, (3814, "Orange Jelly", 56)
, (1111, "Hula Loops", 21)
, (1112, "Big Hula Loops", 122)
, (1234, "Dry Sherry", 540) ]
type TillType = [BarCode]
type BillType = [(Name, Price)]
formatPence :: Price -> String
formatPence _price = show (div _price 100) ++ "." ++ if mod _price 100 >= 10
then show (mod _price 100)
else "0" ++ show (mod _price 100)
formatLine :: (Name, Price) -> String
formatLine (_n, _p) = _n ++ concat ["." | _ <- [1 .. lineLength - length _n - length (formatPence _p)]]
++ formatPence _p ++ "\n"
where
lineLength :: Int
lineLength = 30
formatLines :: [(Name, Price)] -> String
formatLines list = concat [formatLine line | line <- list]
makeTotal :: BillType -> Price
makeTotal _tp = (sum . snd . unzip) _tp - makeDiscount _tp
formatTotal :: Price -> String
formatTotal num = "\nTotal" ++ concat ["." | _ <- [1 .. lineLength - (length . formatPence) num - 5]]
++ formatPence num
where
lineLength :: Int
lineLength = 30
look :: Database -> BarCode -> (Name, Price)
look _db _bc = if placelist _db _bc /= []
then (mid3 (_db !! head (placelist _db _bc)), lst3 (_db !! head (placelist _db _bc)))
else ("Unknown Item", 0)
where
placelist :: Database -> BarCode -> [Int]
placelist _db' _bc' = [i | i <- [0 .. length _db' - 1], fst3 (_db' !! i) == _bc']
fst3 :: (a, b, c) -> a
fst3 (_t, _, _) = _t
mid3 :: (a, b, c) -> b
mid3 (_, _t, _) = _t
lst3 :: (a, b, c) -> c
lst3 (_, _, _t) = _t
lookup :: BarCode -> (Name, Price)
lookup = look database
where
database :: Database
database = codeIndex
makeBill :: TillType -> BillType
makeBill _tp = [lookup bc | bc <- _tp]
makeDiscount :: BillType -> Price
makeDiscount _tp = 100 * div (length [i | i <- _tp, fst i == dsctgt]) satisfytgt
where
dsctgt :: String
dsctgt = "Dry Sherry"
satisfytgt :: Int
satisfytgt = 2
formatDiscount :: Price -> String
formatDiscount _pr = "\nDiscount" ++ concat ["." | _ <- [1 .. lineLength - (length . formatPence) _pr - 8]]
++ formatPence _pr ++ "\n"
where
lineLength :: Int
lineLength = 30
formatBill :: BillType -> String
formatBill bill = formatLines bill ++ (formatDiscount . makeDiscount) bill
++ (formatTotal . makeTotal) bill ++ "\n"
formatTill :: TillType -> String
formatTill = formatBill . makeBill
data Suit = Spade | Heart | Diamond | Club
deriving (Show, Eq)
data Value = One | Two | Three | Four | Five | Six |
Seven | Eight | Nine | Ten | Jack | Queen |
King | Ace
deriving (Show, Eq, Ord)
data Card = CardConstruct Suit Value
deriving (Show, Eq)
type Deck = [Card]
data Player = East | South | West | North
deriving (Show, Eq, Read)
data Action = ActConstruct Card Player
deriving (Show, Eq)
type Trick = [Action]
actionCard :: Action -> Card
actionCard (ActConstruct _card _) = _card
actionPlayer :: Action -> Player
actionPlayer (ActConstruct _ _player) = _player
cardSuit :: Card -> Suit
cardSuit (CardConstruct _suit _) = _suit
cardValue :: Card -> Value
cardValue (CardConstruct _ _value) = _value
maxAndIndex :: [Value] -> (Value, Int)
maxAndIndex _ls = (maximum _ls, head [index | index <- [length _ls - 1, length _ls - 2 .. 0],
_ls !! index == maximum _ls])
winNT :: Trick -> Player
winNT _list = actionPlayer (_list !! winnerIndex _list (cardSuit $ actionCard $ winSuitAction _list))
where
winSuitAction :: Trick -> Action
winSuitAction = head
winSatisList :: Trick -> Suit -> Trick
winSatisList _ls _suit = [act | act <- _ls, (cardSuit . actionCard) act == _suit]
winSatisValue :: Trick -> Suit -> [Value]
winSatisValue _ls _suit = [(cardValue . actionCard) act
| act <- winSatisList _list ((cardSuit . actionCard . winSuitAction) _list)]
winnerIndex :: Trick -> Suit -> Int
winnerIndex _ls _suit = snd (maxAndIndex (winSatisValue _ls _suit))
winT :: Trick -> Suit -> Player
winT _list _suit = head [actionPlayer act | act <- _list, (cardSuit . actionCard) act == _suit]
data Hand = HandConstruct [Card] Player
deriving (Show, Eq)
handPlayer :: Hand -> Player
handPlayer (HandConstruct _ _player) = _player
handCards :: Hand -> [Card]
handCards (HandConstruct _cards _) = _cards
type Hands = [Hand]
checkPlay :: Hands -> Trick -> Bool
checkPlay _hands _trick = allInside _hands _trick && seqTotalCheck _trick _hands
where
collect :: Trick -> Player -> Hand
collect _tr _player = HandConstruct [actionCard act | act <- _tr, actionPlayer act == _player] _player
allCollect :: Trick -> Hands -> Hands
allCollect trick _hands' = [collect trick i | i <- [handPlayer i | i <- _hands']]
valueTable :: [Value]
valueTable = [One, Two, Three, Four, Five, Six,
Seven, Eight, Nine, Ten, Jack, Queen, King, Ace]
countValue :: Value -> Hand -> Int
countValue val _hand = length [i | i <- handCards _hand, cardValue i == val]
countValueList :: Hand -> [Int]
countValueList _hand = [countValue val _hand | val <- valueTable]
compareOrigColl :: Hand -> Hand -> Bool
compareOrigColl orig coll = and [(countValueList orig) !! i >= (countValueList coll) !! i | i <- [0 .. 13]]
hashTable :: Hands -> Hands -> Bool
hashTable _hands _collect = and [compareOrigColl (_hands !! i) (_collect !! i)
| i <- [0 .. length _hands - 1]]
allInside :: Hands -> Trick -> Bool
allInside _hands _trick = hashTable _hands (allCollect _trick _hands)
seqCheck :: Action -> Action -> Hands -> Bool
seqCheck _prev _next _store = if suitSame _prev _next
then True
else not (findSuit (findHand _store (actionPlayer _next))
((cardSuit . actionCard) _prev))
-- if same then True to continue, not True then check whether it has the prev suit, has then False
findHand :: Hands -> Player -> Hand
findHand _stor _play = head [hand | hand <- _stor, handPlayer hand == _play]
suitSame :: Action -> Action -> Bool
suitSame _prev _next = (cardSuit . actionCard) _prev == (cardSuit . actionCard) _next
findSuit :: Hand -> Suit -> Bool
findSuit _hand _suit = and [cardSuit card == _suit | card <- handCards _hand]
seqTotalCheck :: Trick -> Hands -> Bool
seqTotalCheck _trick _hands = and [seqCheck (_trick !! i) (_trick !! (i - 1)) _hands
| i <- [1 .. length _trick - 1]]