-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise5.hs
More file actions
290 lines (203 loc) · 5.7 KB
/
Copy pathExercise5.hs
File metadata and controls
290 lines (203 loc) · 5.7 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
import qualified Data.Char as C
import qualified Data.List as L
import Ex5_10
-- 5.1
maxOccurs :: Int -> Int -> (Int,Int)
maxOccurs a b =
let occ m n = if m == n then 2 else 1
in (max a b, occ a b)
maxThreeOccurs :: Int -> Int -> Int -> (Int, Int)
maxThreeOccurs a b c =
let howManyNotUnique m n p
| threeEqual m n p = 3
| threeNotEqual m n p = 1
| otherwise = 2
threeEqual a b c =
a==b && b==c
threeNotEqual a b c =
a/=b && a/=c && b/=c
maxThree a b c
| a>=b && a>=c = a
| b>=c = b
| otherwise = c
in (maxThree a b c, howManyNotUnique a b c)
-- 5.2
orderTriple :: (Int,Int,Int) -> (Int,Int,Int)
orderTriple (f,s,t) =
let maxThree a b c
| a>=b && a>=c = a
| b>=c = b
| otherwise = c
middle x y z
| between y x z = x
| between x y z = y
| otherwise = z
between m n p = (n >= m && n <= p) || (n <= m && n >= p)
minThree a b c
| a<=b && a<=c = a
| b<=c = b
| otherwise = c
in
(minThree f s t, middle f s t, maxThree f s t)
-- 5.3
-- [(3,x), (7,y), (6, =)]
findXaxis :: [(Int,Char)] -> (Int, Int)
findXaxis [(a,_),_,(b,_)] =
(b `div` a , 0)
-- 5.4
type Equation = [PartOfEq]
type PartOfEq = (Int,Char)
type Coordinates = (Int, Int)
findXaxis1 :: Equation -> Coordinates
findXaxis1 [(a,_),_,(b,_)] =
(b `div` a , 0)
-- 5.8
doubleAll :: [Int] -> [Int]
doubleAll xs = [ x*2 | x <- xs]
-- 5.9
capitalize :: String -> String
capitalize xs = [C.toUpper x | x <- xs]
capitalizeLetters :: String -> String
capitalizeLetters xs = [C.toUpper x | x <- xs , C.isLetter x]
-- 5.10
-- in Ex5_10.hs
-- 5.11
{-
matches 1 [1,2,1,4,5,1] --i [1,1,1]
matches 1 [2,3,4,6] []
-}
matches :: Int -> [Int] -> [Int]
matches n list = foldr (\x acc -> if x == n then x:acc else acc) [] list
{-
elem I [1,2,1,4,5,1] --t True
elem 1 [2,3,4,6] --t False
-}
elem' :: Int -> [Int] -> Bool
elem' x xs = (>0) . length . matches x $ xs
-- 5.13
type Person = String
type Book = String
type Database = [(Person, Book)]
exampleBase :: Database
exampleBase = [("Alice" , "Tintin") , ("Anna" , "Little Women"),
("Alice" , "Asterix") , ("Rory" , "Tintin")]
books :: Database -> Person -> [Book]
books db prsn = [book | (person,book) <- db , person == prsn]
borrowers :: Database -> Book -> [Person]
borrowers db bk = [person | (person,book) <- db , book == bk]
numBorrowed :: Database -> Person -> Int
numBorrowed db prsn = length . books db $ prsn
isBorrowed :: Database -> Book -> Bool
isBorrowed db bk = (>0) . length . borrowers db $ bk
makeLoan :: Database -> Person -> Book -> Database
makeLoan db prsn bk = [(prsn , bk)] ++ db
returnLoan :: Database -> Person -> Book -> Database
returnLoan db prsn bk = [pair | pair <- db , pair /= (prsn , bk)]
-- 5.15
-- will do something like this
-- but concat is kinda crap-shit bycicle
-- !!! in need of reforge :
-- refactor list of lists or make cool filter
type NewDB = [(Person , [Book])]
newBooks :: NewDB -> Person -> [Book]
newBooks db prsn = [concat $ books | (person,books) <- db , person == prsn]
-- 5.16
{-
snd :: (a,b) -> b
snd (x,y) = y
sing :: a -> [a]
sing x = [x]
-}
-- 5.17
{-
bc input and output is the same
[[a]] -> [[a]]
more general form is [a] -> [a]
-}
-- 5.18
{-
general form would look like this
((a,a) , a) -> (a , (a,a))
bu whad bout thAt
(a)->(a)
or even THAT
a -> a
???
-}
-- 5.20
-- !!! in need of reforge
romanDigit :: Char -> String
romanDigit a = toRoman . C.digitToInt $ a
toRoman :: Int -> String
toRoman n
| 0 < n && n < 4 = replicate n 'I'
| n == 4 = "IV"
| 5 < n && n < 9 = "V"++(replicate (n - 5) 'I')
| n == 9 = "IX"
| otherwise = "V"
-- 5.21
-- !!! in need of reforge
onThreeLines :: String -> String -> String -> String
onThreeLines a b c = a++"\n"++b++"\n"++c++"\n"
-- 5.22
onSeparateLines :: [String] -> String
onSeparateLines = concat . map (++"\n")
-- 5.23
duplicate :: String -> Int -> String
duplicate str n
| n < 0 = ""
| n ==1 = str
| otherwise = concat . replicate n $ str
-- 5.24
linelength = 12
strlength :: String -> Bool
strlength = (==linelength) . length
pushRight :: String -> String
pushRight str = if strlength str
then str else (concat $ replicate ((-) linelength $ length str) " ")++str
-- 5.25
{-
what if the linelength is less than length of the string
-}
-- 5.26
{-
> putStr (fibTable 6)
n fib n
0 0
1 1
2 1
3 2
4 3
5 5
6 8
-should use \t and \n ---- used
-test how it works ---- doesn't work
-"n" ++ pushRight "fib n" ---- works perfectly
-implement fib function ++ fib function that produces tuple? list of tuple?
-test simple function of \t \n with two variables
-bild mega function
-}
fibTable :: Int -> String
fibTable n = "kek"
fib :: Int -> Int
fib n
| n == 0 = 0
| n == 1 = 1
| otherwise = fib (n-2) + fib (n-1)
fibTest :: Int -> IO()
fibTest n =
helper n 0 ("n" ++ (pushRight $ "fib n") ++ "\n")
helper :: Int -> Int -> String -> IO()
helper n num los =
let saved = (num, fib num)
in
if num > n
then putStrLn los
else helper n (num+1) (los ++ ((show $ fst saved) ++ (pushRight . show $ snd saved) ++ "\n"))
{-
let saved = (n, fib n)
in (++) (show $ fst saved) $ (pushRight . show $ snd saved)
(show $ fst saved) ++ (pushRight . show $ snd saved) ++ "\n"
-}
-- 5.27
-- reforge database function to deal with more readable output