-
Notifications
You must be signed in to change notification settings - Fork 0
/
two.hs
374 lines (241 loc) · 5.72 KB
/
two.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
module Two where
import Data.List
--1.a
null' [] = True
null' [_]= False
--pembatas
take' 0 _=[]
take' _ []=[]
take' n (x:xs)= x : take' (n-1) (xs)
--pembatas
drop' 0 (x:xs) = (x:xs)
drop' _ [] = []
drop' n (x:xs) = drop' (n-1) (xs)
--pembatas
fst' (a,b) = a
--pembatas
snd' (a,b) = b
--pembatas
map' f (x:xs)= (f x) : map' f (xs)
map' f[]= []
--pembatas
filter' (f) [] = []
filter' (f) (x:xs)
|f x == True = x : filter' f (xs)
|otherwise = filter' f (xs)
--pembatas
delete' n (x:xs)
|n==x = xs
|n/=x = x : delete' n(xs)
|n/=x = (x:xs)
--pembatas
deleteall' n (x:xs)
|n==x = deleteall' n (xs)
|n/=x = x : deleteall' n (xs)
deleteall' n [] = []
--pembatas
foldl'' f n [] = n
foldl'' f n (x:xs) = f (foldl'' f n xs) x
--pembatas
foldl1'' f [x] = x
foldl1'' f (x:xs)= foldl1'' f (f (x) (head' xs): k(xs))
--pembatas
zip' (x':xs') (x:xs) = (x',x) : zip' (xs') (xs)
zip' [] (x:xs) = []
zip' (x:xs) [] = []
zip' [] [] = []
--pembatas
zipWith' f [] (x:xs) = []
zipWith' f (x':xs') [] = []
zipWith' f [] [] = []
zipWith' f (x':xs') (x:xs)= f x' x : zipWith' f (xs') (xs)
--pembatas
nth (x:xs) 0 = (x)
nth (x:xs) n = nth (xs) (n-1)
--pembatas
scanl'' f n [] =[n]
scanl'' f n (x:xs)= [n] ++ scanl'' f (f n x) xs
--pembatas
--bantuan
k [] = []
k (x:xs)=xs
--pembatas
scanl1'' f [x] = [x]
scanl1'' f (x:xs) = x : scanl1'' f ((f x (head' xs)): k(xs) )
--pembatas
elem' n[]= False
elem' n (x:xs)
|n /= x = elem' n (xs)
|otherwise= True
--pembatas
notElem' n[]= True
notElem' n (x:xs)
|n /= x = notElem' n (xs)
|otherwise = False
--pembatas
head' (x:xs) = x
--pembatas
length' [] = 0
length' (x:xs) = 1 + length' (xs)
--pembatas
reverse' [] = []
reverse' (x:xs)= reverse' (xs) ++ x :[]
--pembatas
last' [x] = x
last' (x:xs) = last' (xs)
--pembatas
tail' (x:xs) = (xs)
--pembatas
init' [x] = []
init' (x:xs)= x : init' (xs)
--pembatas
max' a b
|a<b = b
|otherwise = a
--pembatas
min' a b
|a<b = a
|otherwise = b
--pembatas
concat' [x] = x
concat' (x:xs) = x ++ concat' xs
--pembatas
intersperse' n [] = []
intersperse' n (x:xs) = n : x : intersperse' n(xs)
--pembatas
intercalate' [] [x'] = x'
intercalate' [] (x':xs') = x' ++ intercalate' [] xs'
intercalate' (x:xs) [x']= x'
intercalate' (x:xs)(x':xs') = x' ++ (x:xs) ++ intercalate' (x:xs) xs'
--pembatas
and' [] =True
and' (fx:fxs)
|fx== False= False
|otherwise = and' (fxs)
--pembata
or' [] =False
or' (fx:fxs)
|fx == True = True
|otherwise = or' (fxs)
--pembatas
zip3' (x':xs') (x'':xs'')(x:xs) = (x',x'',x) : zip3' (xs') (xs'') (xs)
zip3' [] (x'':xs'')(x:xs) =[]
zip3' (x':xs') [](x:xs)=[]
zip3' (x':xs') (x'':xs'')[] =[]
zip3' [] [] (x:xs)=[]
zip3' [] (x'':xs'')[] = []
zip3' (x':xs') [][] = []
zip3' [] [] [] =[]
--pembatas
sum' [] = 0
sum' (x:xs) = x + sum' xs
-- pembatas
product' [] = 1
product' (x:xs) = x*product' (xs)
--pembatas
words' [x] = [[x]]
words' (x:xs)
|x == ' ' = words' (xs)
|x == '\n' = words' (xs)
|otherwise = [x] : words' (xs)
--(masih salah) --words' "we d a \n g ja he"
--["w","e","d","a","g","j","a","h","e"]
--malah gini
--pembatas
--lines' []= [[]]
--lines' (x:xs)
--x == '\n' = [] : lines' (xs)
---otherwise = x : lines' (xs)
--pembatas
unlines' [] = ""
unlines' (x:xs) = x ++ "\n" ++ (unlines' xs)
--pembatas
unwords' [[x]] = [x]
unwords' (x:xs)= x ++ " " ++ unwords' (xs)
--pembatas
takeWhile' f [] = []
takeWhile' f (x:xs)
| (f x)== True = x : takeWhile' f (xs)
| (f x)== False = []
--pembatas
dropWhile' f [] = []
dropWhile' f (x:xs)
| (f x) == True = dropWhile' f (xs)
| (f x) == False = (x:xs)
--pembatas
concatMap' f [x] = f x ++ []
concatMap' f (x:xs) = f x ++ concatMap' f (xs)
--pembatas
all' f [] = True
all' f (x:xs)
| (f x) == False = False
| otherwise = all' f (xs)
--pembatas
any' f [] = False
any' f (x:xs)
| (f x) == True =True
| otherwise = any' f (xs)
--pembatas
insert' n [] = [n]
insert' n (x:xs)
|n <= x = n : (x:xs)
|n > x = x : insert' n (xs)
--pembatas
zipWith3' f (x':xs') (x'':xs'')(x:xs) =(f x' x'' x) : zipWith3' f (xs') (xs'')(xs)
zipWith3' f [] (x'':xs'')(x:xs) = []
--pembatas
nub' []=[]
nub' (x:xs) = [x] ++ nub' (deleteall' x (xs))
--pembatas
sort' [] =[]
sort' (x:xs) =minimum (x:xs) : sort' (delete' (minimum (x:xs)) (x:xs))
--pembatas
minimum' [x] = x
minimum' (x:xs) = minimum' ((min' x (head' xs)) : tail' xs)
--pembatas
maximum' [x] = x
maximum' (x:xs) = maximum' ((max' x(head' xs )) : tail' xs)
--pembatas
inits' [] = [[]]
inits' (x:xs) = inits' (init' (x:xs)) ++ [x:xs]
--pembatas
tails' [] = [[]]
tails' (x:xs)= (x:xs) : tails' (xs)
--pembatas
union' (x:xs) (x':xs')= nub' ((x:xs) ++ (x':xs'))
--pembatas
intersect' [] (x':xs') = []
intersect' (x:xs) (x':xs')= same x (x':xs') ++ intersect' (xs) (x':xs')
where same n [] = []
same x (x':xs')
|x == x' = [x']
|x /= x' = same x (xs')
--pembatas
group' [] = []
group' (x:xs) = ([x] ++ takeWhile' (x==) (xs)) : group' (dropWhile' (x==)(xs))
--pembatas
splitAt' n (x:xs) =(take' n (x:xs) , drop' n (x:xs))
--pembatas
partition' f (x:xs)= (filter' f (x:xs) , unfilter f (x:xs))
where unfilter (f) [] = []
unfilter (f) (x:xs)
|f x == True = unfilter f (xs)
|f x == False = x : unfilter f (xs)
--pembatas
replicate' 0 b = []
replicate' a b = b : replicate' (a-1) b
--pembatas
--fungsi-------------------------------------------------------------
sumk lst = helper lst 0
where helper [] res = res
helper (x:xs) res = helper xs (x+res)
sol1 lim = iter 1 0
where iter i result
| i > lim = result
| rem i 3 == 0 || rem i 5 == 0 = iter (i+1) (result + i)
| otherwise = iter (i+1) result
fak x
|x==0 = 1
|x>0 = x * fak (x-1)
fairy [] = []
fairy (x:xs) = fak x : fairy xs