-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter7.hs
228 lines (163 loc) · 5.66 KB
/
chapter7.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
-- import Data.Char
-- import Data.List
type Bit = Int
-- bit2int :: [Bit] -> Int
-- bit2int = foldr (\x y -> x + 2 * y) 0
-- -- bit2int bits = sum [ w * b | (w,b) <- zip weights bits]
-- -- where weights = iterate (*2) 1
-- int2bin :: Int -> [Bit]
-- -- int2bin 0 = []
-- -- int2bin n = n `mod` 2 : int2bin (n `div` 2)
-- make8 :: [Bit] -> [Bit]
-- make8 bits = take 8 (bits ++ repeat 0)
-- encode :: String -> [Bit]
-- encode = concat . map (make8 . int2bin . ord)
-- decode :: [Bit] -> String
-- decode = map (chr . bit2int) . chop8
-- transmit :: String -> String
-- transmit = decode . channel . encode
-- channel :: [Bit] -> [Bit]
-- channel = id
-- --Votes
-- votes :: [String]
-- votes = ["Red", "Blue", "Green", "Blue", "Blue", "Red"]
-- count :: Eq a => a -> [a] -> Int
-- count x = length . filter (==x)
-- rmdups :: Eq a => [a] -> [a]
-- rmdups [] = []
-- rmdups (x:xs) = x : filter (/= x) (rmdups xs)
-- result :: Ord a => [a] -> [(Int, a)]
-- result vs = sort [ (count x vs, x) | x <- rmdups vs ]
-- winner :: Ord a => [a] -> a
-- winner = snd . last . result
-- ballots :: [[String]]
-- ballots = [["Red", "Green"],
-- ["Green"],
-- ["Blue"],
-- ["Green", "Red", "Blue"],
-- ["Blue", "Green", "Red"]]
-- rmempty :: Ord a => [[a]] -> [[a]]
-- rmempty = filter (/= [])
-- elim :: Ord a => a -> [[a]] -> [[a]]
-- elim x = map (filter (/= x))
-- rank :: Ord a => [[a]] -> [a]
-- rank = map snd . result . map head
-- winner' :: Ord a => [[a]] -> a
-- winner' bs = case rank (rmempty bs) of
-- [c] -> c
-- (c:cs) -> winner' (elim c bs)
-- -- Exercises
-- -- [f x | x <- xs, p x]
-- -- func x = map (filter p x)
-- -- map f (filter p xs)
-- -- myall :: (a -> Bool) -> [Bool] -> Bool
-- myall p = and . map p
-- -- any' :: (a -> Bool) -> [Bool] -> Bool
-- any' p = or . map p
-- takeWhile' :: (a -> Bool) -> [a] -> [a]
-- takeWhile' _ [] = []
-- takeWhile' p (x:xs) | p x = x : takeWhile' p xs
-- | otherwise = []
-- dropWhile' :: (a -> Bool) -> [a] -> [a]
-- dropWhile' _ [] = []
-- dropWhile' p (x:xs) | p x = dropWhile' p (tail xs)
-- | otherwise = x : xs
-- filter' p = foldr (\x xs -> if p x then x : xs else xs) []
-- dec2int :: [Int] -> Int
-- dec2int = foldl (\x y -> 10*x + y) 0
-- -- a=(\x y -> 10*x + y)
-- -- 0 `a` 1 `a` 2
-- -- 12
-- -- 0 `a` 1 `a` 2 `a` 3
-- -- 123
-- -- 0 `a` 1
-- -- 1
-- -- foldl :: (a -> b -> a) -> a -> [b] -> a
-- -- foldl f v [] = v
-- -- foldl f v (x:xs) = foldl f (f v x) xs
-- curry' :: ((a,b) -> c) -> (a->b->c)
-- curry' f = \x y -> f (x,y)
-- uncurry' :: (a->b->c) -> ((a,b) -> c)
-- uncurry' f = \(x,y) -> f x y
-- unfold :: (a -> Bool) -> (a -> a) -> (a -> a) -> a -> [a]
-- unfold p h t x | p x = []
-- | otherwise = h x : unfold p h t (t x)
-- int2bin = unfold (== 0) (`mod` 2) (`div` 2)
-- chop8 :: [Bit] -> [[Bit]]
-- -- chop8 [] = []
-- -- chop8 bits = take 8 bits : chop8 (drop 8 bits)
-- chop8 = unfold null (take 8) (drop 8)
-- map' :: (a -> b) -> [a] -> [b]
-- -- map' :: (a -> b) -> [a] -> [b]
-- -- map' f xs = [f x | x <- xs]
-- -- map' f = foldr (\x xs -> f x : xs) []
-- map' f = unfold null (f . head) tail
-- iterate' :: (a -> a) -> a -> [a]
-- iterate' = unfold (const False) id
-- x = [f x | x <- xs, p x]
func :: (a -> b) -> [a] -> (a -> Bool) -> [b]
func f xs p = map f $ filter p xs
all' :: Eq a => (a -> Bool) -> [a] -> Bool
all' p xs = length (filter p xs) == length xs
all'' :: Eq a => (a -> Bool) -> [a] -> Bool
all'' p = and . map p
all''' :: Eq a => (a -> Bool) -> [a] -> Bool
all''' p = foldr (\x y -> p x && y) True
all'''' :: Eq a => (a -> Bool) -> [a] -> Bool
all'''' _ [] = True
all'''' p (x:xs) = p x && all'''' p xs
any' :: Eq a => (a -> Bool) -> [a] -> Bool
any' _ [] = False
any' p (x:xs) = p x || any' p xs
any'' :: Eq a => (a -> Bool) -> [a] -> Bool
any'' _ [] = False
any'' p (x:xs) | p x = True
| otherwise = any'' p xs
takeWhile' :: (a -> Bool) -> [a] -> [a]
takeWhile' _ [] = []
takeWhile' p (x:xs) | p x = x : takeWhile' p xs
| otherwise = []
dropWhile' :: (a -> Bool) -> [a] -> [a]
dropWhile' _ [] = []
dropWhile' p (x:xs) | p x = dropWhile' p xs
| otherwise = x:xs
map' :: (a -> b) -> [a] -> [b]
map' f = foldr (\x xs -> f x : xs) []
filter' :: (a -> Bool) -> [a] -> [a]
filter' p = foldr (\x xs -> if p x then x : xs else xs) []
dec2int :: [Int] -> Int
dec2int = foldl (\acc elem -> 10 * acc + elem ) 0
-- fromDigits' :: [Int] -> Int
-- fromDigits' = foldl addDigit 0
-- where addDigit num d = 10*num + d
curry' :: ((a,b) -> c) -> a -> b -> c
curry' f a b = f (a,b)
uncurry' :: (a -> b -> c) -> (a,b) -> c
uncurry' f (a, b) = f a b
unfold :: (a -> Bool) -> (a -> a) -> (a -> a) -> a -> [a]
unfold p h t x | p x = []
| otherwise = h x : unfold p h t (t x)
int2bin :: Int -> [Bit]
int2bin = unfold (== 0) (`mod` 2) (`div` 2)
-- int2bin 0 = []
-- int2bin n = n `mod` 2 : int2bin (n `div` 2)
chop8 :: [Bit] -> [[Bit]]
chop8 = unfold (==[]) (take 8) (drop 8)
-- -- chop8 [] = []
-- -- chop8 bits = take 8 bits : chop8 (drop 8 bits)
altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
altMap _ _ [] = []
altMap f0 f1 (x:xs) = f0 x : altMap f1 f0 xs
luhnDouble :: Int -> Int
luhnDouble x | double > 9 = double - 9
| otherwise = double
where
double = x*2
luhn' :: Int -> Int -> Int -> Int -> Bool
luhn' w x y z = result `mod` 10 == 0
where
result = x + z + luhnDouble w + luhnDouble y
luhn :: [Int] -> Bool
luhn xs = sum (altMap luhnDouble id (reverse xs)) `mod` 10 == 0
-- luhn :: [Int] -> [Int]
-- luhn xs = altMap luhnDouble id xs