-
Notifications
You must be signed in to change notification settings - Fork 0
/
problems11to20.hs
80 lines (64 loc) · 1.76 KB
/
problems11to20.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
data Elem a = Single a | Multiple Int a deriving Show
myPackCE :: Eq a => [a] -> [Elem a]
myPackCE [] = []
myPackCE (x:xs) = packc x 1 xs
packc :: Eq a => a -> Int -> [a] -> [Elem a]
packc a b []
|b==1 = [Single a]
|otherwise = [Multiple b a]
packc a b (x:xs)
|a==x = packc a (b+1) xs
|a/=x && b==1 = (Single a : packc x 1 xs)
|otherwise = (Multiple b a : packc x 1 xs)
myUnpackCE :: Eq a => [Elem a] -> [a]
myUnpackCE [] = []
myUnpackCE ((Single a): xs) = [a] ++ myUnpackCE xs
myUnpackCE ((Multiple b a ): xs) = (replicate b a) ++ myUnpackCE xs
myDup :: [a] -> [a]
myDup [] = []
myDup [a] = [a] ++ [a]
myDup (x:xs) = [x]++[x] ++ myDup xs
myRep :: [a] -> Int -> [a]
myRep [] b = []
myRep (x:xs) b = replicate b x ++ myRep xs b
myDrop :: [a] -> Int -> [a]
myDrop [] b = []
myDrop a 1 = []
myDrop a b = drop' a b 1
drop' :: [a] -> Int -> Int -> [a]
drop' [] b c = []
drop' (x:xs) b c
| b==c = drop' xs b 1
| otherwise = [x] ++ drop' xs b (c+1)
mySplit :: [a] -> Int -> [[a]]
mySplit a b = let k = take b a
in [k,diff a b]
diff :: [a] -> Int -> [a]
diff [] y = []
diff (x:xs) y
| y==0 = (x:xs)
| otherwise = diff xs (y - 1)
mySlice :: [a] -> Int -> Int -> [a]
mySlice x y z = drop y . take z $ x
myRotate :: [a] -> Int -> [a]
myRotate a b
| b < 0 = turnRight a b
| b > 0 = turnLeft a b
| b == 0 = a
where
c = length a
turnRight :: [a] -> Int -> [a]
turnRight a 0 = a
turnRight a b =
let y = tail a ++ [head a]
z = b + 1
in turnRight y z
turnLeft :: [a] -> Int -> [a]
turnLeft a 0 = a
turnLeft a b =
let y = [last a] ++ take (length a - 1) a
z = b - 1
in turnLeft y z
myRemove :: [a] -> Int -> [[a]]
myRemove a 0 = [[head a],tail a]
myRemove a b = [[last (take a b)] , drop a b 1]