-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.hs
More file actions
31 lines (20 loc) · 671 Bytes
/
11.hs
File metadata and controls
31 lines (20 loc) · 671 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
26
27
28
29
30
data Encoding a = Single a | Multiple Int a
deriving (Show)
myEncode :: (Eq a) => [a] -> [Encoding a]
myEncode [] = []
myEncode x = myRunLengthEncoding (myPack x)
myRunLengthEncoding :: (Eq a) => [[a]] -> [Encoding a]
myRunLengthEncoding [] = []
myRunLengthEncoding (x:xs)
| lx == 1 = Single hx:myRunLengthEncoding xs
| otherwise = Multiple lx hx:myRunLengthEncoding xs
where lx = length x
hx = head x
myPack :: (Eq a) => [a] -> [[a]]
myPack [] = []
myPack (x:xs) = myPack' xs [x]
myPack' :: (Eq a) => [a] -> [a] -> [[a]]
myPack' [] r = [r]
myPack' (x:xs) r
| x == (head r) = myPack' xs (x:r)
| otherwise = (r:myPack' xs [x])