-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_20.hs
More file actions
110 lines (82 loc) · 2.44 KB
/
Copy path11_20.hs
File metadata and controls
110 lines (82 loc) · 2.44 KB
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
import Data.List
{--
11. (*) Modified run-length encoding.
Modify the result of problem 10
P11> encodeModified "aaaabccaadeeee"
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']
--}
data EncodingList = Single Char | Multiple Int Char deriving (Show)
encodeModified :: String -> [EncodingList]
encodeModified [] = []
encodeModified xs = map (\x->util (length x) (head x)) $ group xs
where
util l c = if(l > 1) then Multiple l c
else Single c
{--
12. (**) Decode a run-length encoded list.
Given a run-length code list generated as specified
in problem 11. Construct its uncompressed version.
P12> decodeModified [Multiple 4 'a',Single 'b',Multiple 2 'c', Multiple 2 'a',Single 'd',Multiple 4 'e']
"aaaabccaadeeee"
--}
decodeModified :: [EncodingList] -> String
decodeModified = concatMap decodeHelper
where
decodeHelper (Single c) = [c]
decodeHelper (Multiple n c) = replicate n c
{--
14. (*) Duplicate the elements of a list.
> dupli [1, 2, 3]
[1,1,2,2,3,3]
--}
dupli = concatMap (replicate 2)
dupli' [] = []
dupli' (x:xs) = x:x:dupli' xs
{--
15. (**) Replicate the elements of a list a given number of times.
> repli "abc" 3
"aaabbbccc"
--}
repli :: [a]->Int->[a]
repli [] _ =[]
repli (x:xs) n = (replicate n x) ++ repli xs n
repli' xs n = concatMap (replicate n) xs
{--
16. (**) Drop every N'th element from a list.
*Main> dropEvery "abcdefghik" 3
"abdeghk"
--}
dropEvery :: [a]->Int->[a]
dropEvery xs n = [ c | (c, i)<-zip xs [1..], i `mod` n /= 0]
{--
17. (*) Split a list into two parts;
the length of the first part is given.
*Main> split "abcdefghik" 3
("abc", "defghik")
--}
split :: [a]->Int->([a], [a])
split xs n = (take n xs, drop n xs)
{--
18. (**) Extract a slice from a list.
*Main> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7
"cdefg"
--}
slice :: [a] -> Int -> Int -> [a]
slice xs i k = take (k - i + 1) $ drop (i-1) xs
slice'' xs i k = drop (i-1) $ take k xs
slice' (x:xs) i k |k <= 1 = []
|i > 1 = slice' xs (i-1) k
|k > 1 = x : (slice' xs i (k-1))
{--
19. (**) Rotate a list N places to the left.
*Main> rotate ['a','b','c','d','e','f','g','h'] 3
"defghabc"
*Main> rotate ['a','b','c','d','e','f','g','h'] (-2)
"ghabcdef"
--}
rotate :: [a] -> Int -> [a]
rotate xs n | n > 0 = left xs n
| n < 0 = right xs n
where
left ys m =