-
Notifications
You must be signed in to change notification settings - Fork 0
/
h-99.hs
423 lines (317 loc) · 10.7 KB
/
h-99.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
module H99 where
import Data.List(group, sortBy, groupBy)
import Data.Function(on)
import System.Random(randomRIO)
import Control.Monad(replicateM)
import Data.Ord(comparing)
-- Problem 1
myLast :: [a] -> a
myLast [x] = x
myLast (_:xs) = myLast xs
myLast' :: [a] -> a
myLast' = head . reverse
myLast'' :: [a] -> a
myLast'' = foldl1 (flip const)
myLast''' :: [a] -> a
myLast''' = foldr1 (flip const)
-- The following are confusing and amazing.
myLast'''' :: [a] -> a
myLast'''' = foldr1 (const id)
myLast''''' :: [a] -> a
myLast''''' = foldl1 (const id)
-- Problem 2
myButLast :: [a] -> a
myButLast x = reverse x !! 1
myButLast' :: [a] -> a
myButLast' [x, _] = x
myButLast' (_ : xs) = myButLast' xs
myButLast'' :: [a] -> a
myButLast'' = last . init
myButLast''' :: [a] -> a
myButLast''' = head . tail . reverse
-- Problem 3
elementAt :: [a] -> Int -> a
elementAt xs i = xs !! (i - 1)
elementAt' :: [a] -> Int -> a
elementAt' (x : _) 1 = x
elementAt' (_ : xs) i
| i < 1 = error "Index out of bounds"
| otherwise = elementAt' xs (i-1)
elementAt' [] _ = error "Index out of bounds"
elementAt'' :: [a] -> Int -> a
elementAt'' xs i = last . take i $ xs
-- Problem 4
myLength :: [a] -> Int
myLength [] = 0
myLength (_ : xs) = 1 + myLength xs
myLength' :: [a] -> Int
myLength' = sum . map (\x -> 1)
myLength'' :: [a] -> Int
myLength'' = foldl (\n _ -> n+1) 0
myLength''' :: [a] -> Int
myLength''' = foldr (\_ n -> n+1) 0
myLength'''' :: [a] -> Int
myLength'''' = foldr (const (+1)) 0
-- Prbolem 5
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
reverse'' :: [a] -> [a]
reverse'' = foldl (flip (:)) []
reverse''' :: [a] -> [a]
reverse''' list = reverse'''' list []
where reverse'''' [] reversed = reversed
reverse'''' (x:xs) reversed = reverse'''' xs (x:reversed)
-- Problem 6
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome list = list == reverse list
isPalindrome' :: (Eq a) => [a] -> Bool
isPalindrome' [] = True
isPalindrome' [_] = True
isPalindrome' list = (head list) == (last list) && (isPalindrome' $ init $ tail list)
-- Problem 7
data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem x) = [x]
flatten (List x) = concatMap flatten x
flatten' :: NestedList a -> [a]
flatten' (Elem x) = [x]
flatten' (List []) = []
flatten' (List (x:xs)) = flatten' x ++ flatten' (List xs)
flatten'' :: NestedList a -> [a]
flatten'' (Elem x) = [x]
flatten'' (List x) = foldr (++) [] $ map flatten'' x
-- Problem 8
compress :: Eq a => [a] -> [a]
compress = foldr compHelp []
where compHelp x [] = [x]
compHelp x (y:ys)
| x == y = y:ys
| otherwise = x:y:ys
compress' :: Eq a => [a] -> [a]
compress' [] = []
compress' (x:xs) = x : (compress $ dropWhile (== x) xs)
compress'' :: Eq a => [a] -> [a]
compress'' x = foldr (\a b -> if a == (head b) then b else a:b) [last x] x
compress''' :: Eq a => [a] -> [a]
compress''' = map head . Data.List.group
-- Problem 9
pack :: Eq a => [a] -> [[a]]
pack [] = []
pack (x:xs) = packHelper [] [] (x:xs)
where packHelper result [] [] = result
packHelper result current [] = result ++ [current]
packHelper result [] (x:xs) = packHelper result [x] xs
packHelper result current@(y:_) (x:xs)
| y == x = packHelper result (x:current) xs
| otherwise = packHelper (result ++ [current]) [x] xs
pack' :: Eq a => [a] -> [[a]]
pack' = foldr packHelper []
where packHelper item [] = [[item]]
packHelper item result@(x:xs)
| item == head x = (item:x) : xs
| otherwise = [item] : result
pack'' :: Eq a => [a] -> [[a]]
pack'' [] = []
pack'' (x:xs) = let (first, rest) = span (==x) xs
in (x:first) : pack'' rest
-- Problem 10
encode :: Eq a => [a] -> [(Int, a)]
encode list = let grouped = group list
in zip (map length grouped) (map head grouped)
encode' :: Eq a => [a] -> [(Int, a)]
encode' = map (\x -> (length x, head x)) . group
encode'' :: Eq a => [a] -> [(Int, a)]
encode'' = foldr encodeHelper []
where encodeHelper item [] = [(1, item)]
encodeHelper item result@((count, x):xs)
| item == x = (count + 1, x):xs
| otherwise = (1, item) : result
-- Problem 11
data Problem11 a = Single a | Multiple Int a deriving (Show)
encodeModified :: Eq a => [a] -> [Problem11 a]
encodeModified = map transformHelper . encode
where transformHelper (1, x) = Single x
transformHelper (n, x) = Multiple n x
-- Problem 12
decodeModified :: [Problem11 a] -> [a]
decodeModified = concatMap decodeHelper
where decodeHelper (Single x) = [x]
decodeHelper (Multiple n x) = replicate n x
-- Problem 13
encodeDirect :: Eq a => [a] -> [Problem11 a]
encodeDirect = foldr encodeHelper []
where encodeHelper x result@(h:rest)
| (getContent h) == x = (Multiple (1 + getCount h) x) : rest
| otherwise = (Single x) : result
encodeHelper x [] = [(Single x)]
getContent (Single x) = x
getContent (Multiple _ x) = x
getCount (Single _) = 1
getCount (Multiple n _) = n
-- Problem 14
dupli :: [a] -> [a]
dupli = concatMap (\x->[x,x])
dupli' :: [a] -> [a]
dupli' = foldr (\x xs -> x:x:xs) []
-- Problem 15
repli :: [a] -> Int -> [a]
repli xs n = concatMap (replicate n) xs
repli' :: [a] -> Int -> [a]
repli' = flip $ concatMap . replicate
-- Problem 16
dropEvery :: [a] -> Int -> [a]
dropEvery [] _ = []
dropEvery _ 1 = []
dropEvery xs 0 = xs
dropEvery (x:xs) n = dropHelper [x] (n-1) xs
where dropHelper result i [] = result
dropHelper result 1 (_:ys) = dropHelper result n ys
dropHelper result i (y:ys) = dropHelper (result ++ [y]) (i-1) ys
dropEvery' :: [a] -> Int -> [a]
dropEvery' xs 0 = xs
dropEvery' [] _ = []
dropEvery' xs n = map snd $ filter ((n /=) . fst) $ zip (cycle [1..n]) xs
-- Problem 17
split :: [a] -> Int -> ([a], [a])
split xs n = splitHelper [] n xs
where splitHelper list 0 ys = (list, ys)
splitHelper list i [] = (list, [])
splitHelper list i (y:ys) = splitHelper (list ++ [y]) (i-1) ys
split' :: [a] -> Int -> ([a], [a])
split' xs 0 = ([], xs)
split' [] i = ([], [])
split' (x:xs) n = (x:f, l) where (f,l) = split' xs (n-1)
-- Problem 18
slice :: [a] -> Int -> Int -> [a]
slice xs h t = sliceHelper [] xs 1
where sliceHelper l [] _ = l
sliceHelper l (y:ys) i
| i > t = l
| i < h = sliceHelper l ys (i+1)
| otherwise = sliceHelper (l ++ [y]) ys (i+1)
slice' :: [a] -> Int -> Int -> [a]
slice' xs h t = map snd $ filter ((>=h) . fst) $ zip [1..t] xs
slice'' :: [a] -> Int -> Int -> [a]
slice'' [] _ _ = []
slice'' (x:xs) h t
| h > 1 = slice'' xs (h - 1) (t - 1)
| t < 1 = []
| otherwise = x:slice'' xs (h - 1) (t - 1)
-- Problem 19
rotate :: [a] -> Int -> [a]
rotate xs 0 = xs
rotate xs i
| i > len = rotate xs (mod i len)
| i > 0 = swapConcat $ splitAt i xs
| i < 0 = rotate xs $ length xs + i
where swapConcat (h,t) = t ++ h
len = length xs
rotate' :: [a] -> Int -> [a]
rotate' xs 0 = xs
rotate' [] _ = []
rotate' l@(x:xs) i
| i > len = rotate l (mod i len)
| i > 0 = rotate' (xs ++ [x]) (i - 1)
| i < 0 = rotate' l $ length l + i
where len = length l
-- Problem 20
removeAt :: Int -> [a] -> (Maybe a, [a])
removeAt _ [] = (Nothing, [])
removeAt 0 (x:xs) = (Just x, xs)
removeAt k (x:xs) = let (a,r) = removeAt (k-1) xs in (a, x:r)
removeAt' :: Int -> [a] -> (a, [a])
removeAt' k xs = let (h, t) = splitAt (k + 1) xs in (last h, init h ++ t)
-- Problem 21
insertAt :: a -> [a] -> Int -> [a]
insertAt el xs 1 = el:xs
insertAt _ [] _ = []
insertAt el (x:xs) n = x:insertAt el xs (n-1)
insertAt' :: a -> [a] -> Int -> [a]
insertAt' el xs n = let (h,t)=splitAt n xs in h ++ el:t
-- Problem 22
range :: Int -> Int -> [Int]
range f l
| f <= l = [f..l]
| otherwise = reverse $ range l f
range' :: Int -> Int -> [Int]
range' f l
| f == l = [f]
| f < l = f:range' (f+1) l
| f > l = f:range' (f-1) l
-- Problem 23
rnd_select :: [a] -> Int -> IO [a]
rnd_select [] _ = return []
rnd_select xs n
| n <= 0 = error "n must be greater than zero."
| otherwise = replicateM n rand
where rand = do r <- randomRIO (0, (length xs) - 1)
return (xs!!r)
-- Problem 24
diff_select :: Int -> Int -> IO [Int]
diff_select n m = diff_select' n [1..m]
diff_select' :: Int -> [a] -> IO [a]
diff_select' 0 _ = return []
diff_select' _ [] = error "too few elements to choose from"
diff_select' n xs = do r <- randomRIO (0,(length xs) - 1)
let remain = take r xs ++ drop (r+1) xs
rest <- diff_select' (n-1) remain
return ((xs!!r):rest)
-- Problem 25
rnd_permu :: [a] -> IO [a]
rnd_permu xs = diff_select' (length xs) xs
-- Problem 26
combinations :: Int -> [a] -> [[a]]
combinations _ [] = []
combinations 1 xs = map (\x -> [x]) xs
combinations n (x:xs) = combinations n xs ++ (map (x:) $ combinations (n-1) xs)
-- Problem 27
-- helper function
combinationsMore :: Int -> [a] -> [([a], [a])]
combinationsMore 0 xs = [([], xs)]
combinationsMore n [] = []
combinationsMore n (x:xs) = l1 ++ l2
where l1 = [ (x:ys, zs) | (ys, zs) <- combinationsMore (n-1) xs ]
l2 = [ (ys, x:zs) | (ys, zs) <- combinationsMore n xs]
groupDisjoint :: [Int] -> [a] -> [[[a]]]
groupDisjoint [] _ = [[]]
groupDisjoint (n:ns) xs = [ g:gs | (g, rs) <- combinationsMore n xs, gs <- groupDisjoint ns rs]
-- Problem 28
lsort :: [[a]] -> [[a]]
lsort = sortBy (comparing length)
lfsort :: [[a]] -> [[a]]
lfsort = concat . lsort . groupBy ((==) `on` length) . lsort
-- Problem 31
isPrime :: Integral a => a -> Bool
isPrime n
| n < 2 = False
| otherwise = all ((/= 0).mod n) $ 2:[3,5..s]
where s = floor $ sqrt $ fromIntegral n
-- Problem 32
myGCD :: Integer -> Integer -> Integer
myGCD a b
| a < 0 = myGCD (-a) b
| b < 0 = myGCD a (-b)
| b == 0 = a
| otherwise = myGCD b (a `mod` b)
-- Problem 33
coprime :: Integral a => a -> a -> Bool
coprime a b = gcd a b == 1
-- Problem 34
totient :: Int -> Int
totient x = length $ filter (coprime x) [1..(x-1)]
-- Problem 35
primeFactors :: Int -> [Int]
primeFactors x = testDiv x $ 2:[3,5..(floor $ sqrt $ fromIntegral x)]
where testDiv _ [] = []
testDiv target (y:ys)
| target `mod` y == 0 = yys ++ (testDiv targetRemain ys)
| otherwise = testDiv target ys
where (yys, targetRemain) = tryRemove target y
tryRemove m n
| m `mod` n /= 0 = ([], m)
| otherwise = (n:ns, remainM)
where (ns, remainM) = tryRemove (m `div` n) n
-- Problem 36
primeFactorsMult :: Int -> [(Int, Int)]
primeFactorsMult = map (\x -> (head x, length x)) . group . primeFactors