-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion1-10.hs
61 lines (48 loc) · 1.35 KB
/
Question1-10.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
import Data.List
myLast :: [a] -> a
myLast (x:[]) = x
myLast (_:xs) = myLast xs
myButLast :: [a] -> a
myButLast (x:_:[]) = x
myButLast (_:ys) = myButLast ys
elementAt :: [a] -> Int -> a
elementAt (x:_) 1 = x
elementAt (_:xs) i = elementAt xs (i - 1)
myLength :: [a] -> Int
myLength xs = myLength' xs 0
where
myLength' [] y = y
myLength' (_:as) y = myLength' as y+1
reverse' :: [a] -> [a]
reverse' xs = reverse'' xs []
where
reverse'' [] ys = ys
reverse'' (y:ys) zs = reverse'' ys (y:zs)
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome xs = reverse' xs == xs
data NestedList a
= Elem a
| List [NestedList a]
flatten :: NestedList a -> [a]
flatten (List []) = []
flatten (Elem x) = [x]
flatten (List xs) = concatMap flatten xs
compress :: (Eq a) => [a] -> [a]
compress = map head . group
compress' :: (Eq a) => [a] -> [a]
compress' (x:ys@(y:_))
| x == y = compress' ys
| otherwise = x : compress' ys
compress' xs = xs
pack :: (Eq a) => [a] -> [[a]]
pack = group
pack' :: (Eq a) => [a] -> [[a]]
pack' [] = []
pack' (x:xs) = pack'' xs [[x]]
where
pack'' [] bs = bs
pack'' (a:bs) cs
| a == (head . last) cs = pack'' bs $ init cs ++ [a : last cs]
| otherwise = pack'' bs $ cs ++ [[a]]
encode :: (Eq a) => [a] -> [(Int, a)]
encode xs = map (\a -> (length a, head a)) (group xs)