-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise7.hs
More file actions
309 lines (252 loc) · 6.83 KB
/
Copy pathExercise7.hs
File metadata and controls
309 lines (252 loc) · 6.83 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
module Exercise7 where
import Data.List (nub, delete)
import Data.Char (toLower)
import Chapter5
import Prelude hiding (Word, getLine)
firstDigit :: String -> Char
firstDigit st =
case (digits st) of
[] -> '\0'
(x:_) -> x
-- 7.1
firstInteger :: [Int] -> Int
firstInteger ints =
case ints of
(x:y:_) -> x + y
otherwise -> 0
-- 7.2
addFirstTwo :: [a] -> [a]
addFirstTwo xs =
case xs of
(a:b:_) -> [a,b]
(x:_) -> [x]
otherwise -> []
-- 7.3
firstInteger' :: [Int] -> Int
firstInteger' xs =
if length xs >= 2
then head xs + xs !! 1
else 0
addFirstTwo' :: [a] -> [a]
addFirstTwo' xs
| length xs >= 2 = head xs : xs !! 1 : []
| null xs = []
| otherwise = head xs : []
-- 7.4
product' :: [Int] -> Int
product' [] = 1
product' (x:xs) = x * product' xs
-- 7.5
and' :: [Bool] -> Bool
and' [] = True
and' (a:as) = a && and' as
or' :: [Bool] -> Bool
or' [] = False
or' (x:xs) = x || or' xs
iSort :: [Int] -> [Int]
iSort [] = []
iSort (x:xs) = ins x (iSort xs)
where
ins b [] = [b]
ins b (a:as)
| b <= a = b:(a:as)
| otherwise = a : ins b as
-- 7.6
elemNumRc :: Int -> [Int] -> Int
elemNumRc n [] = 0
elemNumRc n (x:xs)
| n == x = 1 + elemNum n xs
| otherwise = elemNumRc n xs
elemNum :: Int -> [Int] -> Int
elemNum x xs = length $ filter (==x) xs
-- 7.7
unique :: [Int] -> [Int]
unique [] = []
unique (x:xs)
| elem x xs = unique $ filter (/=x) xs
| otherwise = x:unique xs
-- 7.8
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
unzip' :: [(a,b)] -> ([a], [b])
unzip' a = (map fst a, map snd a)
-- 7.9
minAndMaxList :: [Int] -> (Int,Int)
minAndMaxList a = (findMin a, findMax a)
where findMin = foldr1 (\n acc -> if n > acc then acc else n)
findMax = foldr1 (\n acc -> if n < acc then acc else n)
minAndMaxList' :: [Int] -> (Int,Int)
minAndMaxList' x = (head done, last done)
where done = iSort x
-- 7.10 and 7.11
-- !!! reforge remove reverse
-- probably change function to fold
ins :: Ord a => a -> [a] -> [a]
ins a as = reversedIns a as
where reversedIns a as =
let
help item [] [] = item:[]
help item (x:xs) list
| item < x = list ++ (x:xs) ++ [item]
| otherwise = help item xs list ++ [x]
in help a as []
-- 7.12
{-
test data for duplicate removing nub $ iSort
1) on empty list
2) list with no duplicates
3) list with two duplicates
-}
-- 7.13
-- !!! TODO
-- list of tuples to lexicographic order i.e (2,73) < (3,0) < (3,2)
lexicOrd [] = []
lexicOrd (x:xs) = [y | y<-xs , (fst y <= fst x && snd y <= snd x)] ++ [x] ++ lexicOrd [y | y<-xs, (fst y > fst x && snd y > snd x)]
qSort :: [Int] -> [Int]
qSort [] = []
qSort (x:xs) = [y | y<-xs , y<=x] ++ [x] ++ qSort [y | y<-xs , y>x]
-- 7.14
drop' :: Int -> [a] -> [a]
drop' n [] = []
drop' 0 ls = ls
drop' n (x:xs) = drop (n-1) xs
splitAt' :: Int -> [a] -> ([a],[a])
splitAt' n ls = (take n ls,drop' n ls)
-- 7.15
-- implement something like this take (-_) _ = error "Negative nummber"
-- 7.16
-- zip3 definition
--recursive one
zip3'r :: [a] -> [b] -> [c] -> [(a,b,c)]
zip3'r a b c
| null a || null b || null c = []
zip3'r (a:as) (b:bs) (c:cs) = [(a,b,c)] ++ zip3'r as bs cs
-- zip one
-- dunno how to add item to a tuple
-- 7.17
-- descending order and removing duplicates
qSortn :: [Int] -> [Int]
qSortn ls = reverse . qSort . nub $ ls
-- 7.18
-- sublist "ship" "Fish & Chips"
{-
"ship" "sh & Chips"
"hip" "h & Chips"
"ip" "ips"
"p" "ps"
"" "s"
-}
isSubList :: Eq a => [a] -> [a] -> Bool
isSubList [] b = True
isSubList a [] = False
isSubList (x:xs) (y:ys)
| x == y = True && isSubList xs ys
| otherwise = isSubList (x:xs) ys
-- subsequence "Chip" "Fish & Chips"
isSubSequence :: Eq a => [a] -> [a] -> Bool
isSubSequence [] _ = True
isSubSequence _ [] = False
isSubSequence (x:xs) (y:ys)
| x == y = subHelper xs ys (x:xs)
| otherwise = isSubSequence (x:xs) ys
where
subHelper [] _ _ = True
subHelper _ [] _ = False
subHelper (x:xs) (y:ys) savedWord
| x == y = True && subHelper xs ys savedWord
| otherwise = isSubSequence savedWord (y:ys)
whitespace = ['\t','\n',' ']
getWord :: String -> String
getWord [] = []
getWord (x:xs)
| elem x whitespace = []
| otherwise = x : getWord xs
dropWord :: String -> String
dropWord [] = []
dropWord (x:xs)
| elem x whitespace = (x:xs)
| otherwise = dropWord xs
dropSpace :: String -> String
dropSpace [] = []
dropSpace (x:xs)
| elem x whitespace = dropSpace xs
| otherwise = (x:xs)
type Word = String
splitWords :: String -> [Word]
splitWords st = split (dropSpace st)
split :: String -> [Word]
split [] = []
split st = (getWord st) : split (dropSpace (dropWord st))
type Line = [Word]
getLine :: Int -> [Word] -> Line
getLine len [] = []
getLine len (w:ws)
| length w <= len = w : restOfLine
| otherwise = []
where
newlen = len - (length w + 1)
restOfLine = getLine newlen ws
-- 7.19
dropLine :: Int -> [Word] -> Line
dropLine len [] = []
dropLine len (w:ws)
| len >= length w = dropLine newlen ws
| otherwise = w:ws
where
newlen = len - (length w +1)
lineLen = 17
splitLines :: [Word] -> [Line]
splitLines [] = []
splitLines ws
= getLine lineLen ws
: splitLines (dropLine lineLen ws)
fill :: String -> [Line]
fill = splitLines . splitWords
-- 7.20
joinLine :: Line -> String
joinLine [] = []
joinLine (w:ws)
| null ws = w ++ joinLine ws
| otherwise = w ++ " " ++ joinLine ws
-- 7.21
joinLines :: [Line] -> String
joinLines = concat . map (\n -> n ++ "\n") . map joinLine
{-putStr $ joinLines $ splitLines $ words "Errors are red, my screen is blue, i think i deleted system32"-}
-- 7.22 skipped
-- think it trash
-- 7.23
-- skipped too bc i don't know how it should looks like
-- 7.24
-- take a string and produce amount of (characters,words,lines)
wc :: String -> (Int,Int,Int)
wc ws = (length ws, length $ splitWords ws, length $ filter (=='\n') ws)
-- 7.25
-- filterPunc -> toLower -> isPalindrome
punctuation = " ,.'!?"
isPalin :: String -> Bool
isPalin = isPalindrome . map toLower . filter (\n -> not $ elem n punctuation)
where
isPalindrome a = a == reverse a
-- 7.26
{-
[How much is that?] []
[How ] [uch is that?] [m]
[How ] [ch is that?] [mu]
[How ] [h is that?] [muc]
[How ] [is that?] [much]
[How ] [is that?] [much ]
[How ] [tall] [ is that?]
-}
-- bogged subst "hate" "love" "Hello, hatiko I hate you"
subst :: String -> String -> String -> String
subst _ _ [] = []
subst [] _ st = st
subst oldSub newSub string = subHelp oldSub newSub string oldSub []
where
--subHelp oldSub newSub nextStr savedSub prevStr
subHelp [] newSub nextStr _ prevStr = prevStr ++ newSub ++ nextStr
subHelp _ _ [] _ prevStr = prevStr
subHelp (x:xs) newSub (y:ys) savedSub prevStr
| x == y = subHelp xs newSub ys savedSub prevStr
| x /= y = subHelp savedSub newSub ys savedSub (prevStr ++ [y])