-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_10.hs
More file actions
175 lines (136 loc) · 3.71 KB
/
Copy path01_10.hs
File metadata and controls
175 lines (136 loc) · 3.71 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Data.List
{--
1.(*) Find the last element of a list.
Prelude> myLast [1,2,3,4]
4
Prelude> myLast ['x','y','z']
'z'
--}
myLast :: [a] -> a
myLast [] = error "empty list!"
myLast (x:[]) = x -- [x] = x
myLast (_:xs) = myLast xs
myLast' [] = error "empty list!"
myLast' xs = xs !! (length xs - 1)
{--
2.(*) Find the last but one element of a list.
Prelude> myButLast [1,2,3,4]
3
Prelude> myButLast ['a'..'z']
'y'
--}
myButLast :: [a] -> a
myButLast [] = error "empty list!"
myButLast [x, _] = x
myButLast (_:xs) = myButLast xs
myButLast' [] = error "empty list!"
myButLast' (x : ( _ : [])) = x
myButLast' (_:xs) = myButLast' xs
{--
3. Find the K'th element of a list.
The first element in the list is number 1.
Prelude> elementAt [1,2,3] 2
2
Prelude> elementAt "haskell" 5
'e'
--}
elementAt :: [a] -> Int -> a
elementAt [] _ = error "empty list"
elementAt xs 0 = error "Index out of bounds"
elementAt (x:_) 1 = x
elementAt (_:xs) k
| k < 1 = error "Index out of bounds"
| otherwise = elementAt xs (k - 1)
elementAt'' :: [a] -> Int -> a
elementAt'' (x:_) 1 = x
elementAt'' (_:xs) i = elementAt'' xs (i - 1)
elementAt'' _ _ = error "Index out of bounds"
{--
4. (*) Find the number of elements of a list.
Prelude> myLength [123, 456, 789]
3
Prelude> myLength "Hello, world!"
13
--}
myLength :: [a] -> Int
myLength [] = 0
myLength (_:xs) = 1 + myLength xs
myLength' :: [a] -> Int
myLength' xs = sum [1 | _<- xs]
myLength'' ::[a] -> Int
myLength'' = sum . map (\_->1)
{--
5. (*) Reverse a list.
Prelude> myReverse "A man, a plan, a canal, panama!"
"!amanap ,lanac a ,nalp a ,nam A"
Prelude> myReverse [1,2,3,4]
[4,3,2,1]
--}
myReverse :: [a] -> [a]
myReverse [] = []
myReverse (x:xs) = myReverse xs ++ [x]
reverse' :: [a] -> [a]
reverse' list = reverse'' list []
where
reverse'' [] reversed = reversed
reverse'' (x:xs) reversed = reverse'' xs (x:reversed)
{--
6. (*) Find out whether a list is a palindrome.
A palindrome can be read forward or backward;
e.g. (x a m a x).
*Main> isPalindrome [1,2,3]
False
*Main> isPalindrome "madamimadam"
True
*Main> isPalindrome [1,2,4,8,16,8,4,2,1]
True
--}
--isPalindrome :: [a] -> Bool
isPalindrome xs = reverse xs == xs
isPalindrome' [] = True
isPalindrome' [_] = True
isPalindrome' (x:xs) = x == last xs && isPalindrome' (init xs)
{--
7. (**) Flatten a nested list structure.
We have to define a new data type,
because lists in Haskell are homogeneous.
data NestedList a = Elem a | List [NestedList a]
*Main> flatten (Elem 5)
[5]
*Main> flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]])
[1,2,3,4,5]
*Main> flatten (List [])
[]
--}
data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem x) = [x]
flatten (List (x:xs)) = flatten x ++ flatten (List xs)
flatten (List []) = []
{--
8. (**) Eliminate consecutive duplicates of list elements.
> compress "aaaabccaadeeee"
"abcade"
--}
compress ::(Eq a)=>[a] -> [a]
compress [] =[]
compress (x:xs) = x : (compress (dropWhile (== x) xs))
compress' xs = map head $ group xs
{--
9. (**) Pack consecutive duplicates of list elements into sublists.
If a list contains repeated elements they should be placed
in separate sublists.
*Main> pack ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']
["aaaa","b","cc","aa","d","eeee"]
--}
--pack :: String->[String]
pack [] = []
pack (x:xs) = (x: takeWhile (==x) xs) : pack (dropWhile (==x) xs)
{--
10. (*) Run-length encoding of a list. Use the result of
problem P09 to implement the so-called run-length encoding
data compression method.
encode "aaaabccaadeeee"
[(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')]
--}
encode xs = map (\x->(length x, head x)) $ pack xs