-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasks1.hs
75 lines (63 loc) · 2.12 KB
/
Tasks1.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
module Tasks1 where
-- #1
myLast :: [a] -> a
myLast list = case list of [] -> error "There is no last element for empty list"
[x] -> x
(_:xs) -> myLast xs
-- #2
myButLast :: [a] -> a
myButLast [] = error "No one but last element for empty list"
myButLast (_:[]) = error "No one but last element for single element list"
myButLast (a:_:[]) = a
myButLast (_:xs) = myButLast xs
-- #3
elementAt :: (Integral b) => [a] -> b -> a
elementAt (x:xs) index
| index > 1 = elementAt xs (index - 1)
| otherwise = x
elementAt [] 1 = error "Index out of bounds"
-- #4
myLength :: (Num b) => [a] -> b
myLength [] = 0
myLength (_:xs) = 1 + myLength xs
-- #5
myReverse :: [a] -> [a]
myReverse (elem:[]) = elem:[]
myReverse [] = []
myReverse (x:xs) = myReverse xs ++ (x:[])
-- #6
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome [] = True
isPalindrome (_:[]) = True
isPalindrome (x:xs) = (x == last xs) && isPalindrome (init xs)
-- #7
data NestedList a = Elem a | List [NestedList a] deriving (Show)
myFlatten :: NestedList a -> [a]
myFlatten (Elem a) = [a]
myFlatten (List []) = []
myFlatten (List (x:xs)) = myFlatten x ++ myFlatten (List xs)
-- #8
compress :: (Eq a) => [a] -> [a]
compress [] = []
compress [a] = [a]
compress (a:b:xs)
| a == b = compress (b:xs)
| otherwise = a : (compress (b:xs))
-- #9
pack :: (Eq a) => [a] -> [[a]]
pack [a] = [[a]]
pack list = packWithAccumulator list []
where packWithAccumulator :: (Eq a) => [a] -> [a] -> [[a]]
packWithAccumulator [a] acc = [a:acc]
packWithAccumulator (a:b:xs) acc
| a == b = packWithAccumulator (b:xs) (a:acc)
| otherwise = (a:acc) : (packWithAccumulator (b:xs) [])
-- #10
encode :: (Eq a, Num b) => [a] -> [(b,a)]
encode [] = error "Encode cannot be performed on empty list"
encode list = encodeWithCount list 1
where encodeWithCount :: (Eq a, Num b) => [a] -> b -> [(b,a)]
encodeWithCount [a] count = [(count, a)]
encodeWithCount (a:b:xs) count
| a == b = encodeWithCount (b:xs) (count+1)
| otherwise = (count, a) : (encodeWithCount (b:xs) 1)