-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPart4.hs
More file actions
25 lines (20 loc) · 804 Bytes
/
Part4.hs
File metadata and controls
25 lines (20 loc) · 804 Bytes
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
module Part4 where
runLengthEncode :: Eq a => [a] -> [(a, Int)]
runLengthEncode [] = []
runLengthEncode (x:xs) = nextGroup x 1 xs
where
nextGroup e n [] = [(e, n)]
nextGroup e n (y:ys)
| e == y = nextGroup e (n + 1) ys
| otherwise = (e, n) : nextGroup y 1 ys
rlePropLengthPreserved :: [Int] -> Bool
rlePropLengthPreserved as = length as == (sum $ map snd $ runLengthEncode as)
rlePropDupesCollapsed :: Int -> Bool
rlePropDupesCollapsed n
| m == 0 = runLengthEncode "" == []
| otherwise = runLengthEncode (replicate m 'x') == [('x', m)]
where m = n `mod` 100
rlePropRoundTrip :: [Int] -> Bool
rlePropRoundTrip ns = runLengthEncode xs == is
where is = zip ['a'..] $ map (\n -> n `mod` 100 + 1) ns
xs = concatMap (\(i,n) -> replicate n i) is