-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataTypePractice.hs
More file actions
595 lines (376 loc) · 8.48 KB
/
Copy pathdataTypePractice.hs
File metadata and controls
595 lines (376 loc) · 8.48 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
filter (<3) [1..5]
-- I had errored in not wrapping the x in []
-- also seems to work with
:{
let quicks [] = []
quicks (x:xs) = quicks left ++ [x] ++ quicks right
where left = filter (<x) xs
right = filter (>x) xs
:}
:t quicks
quicks [1,5,3,2]
head [[1,5,3,2],[1,5,3,2]]
let hLaGoE c x a b = head $ lessAndGreater c x a b
let thLaGoE c x a b = head $ tail $ lessAndGreater c x a b
thLaGoE 3 [9,4,1,5,3,2] [] []
lessAndGreater 3 [9,4,1,5,3,2] [] []
let hLGE c x = hLaGoE c x [] []
let thLGE c x = thLaGoE c x [] []
hLGE 3 [9,4,1,5,2]
thLGE 3 [9,4,1,5,2]
let asdf (x:xs) = hLGE x xs
asdf [3,2,4,1,5]
asdf [3,2]
asdf [3,2]
let fdsa (x:xs) = thLGE x xs
fdsa [3]
fdsa []
thLGE 3 [11,9,4,1,5,2]
-- bunch of mistakes were made, but I finally resolved it by just having the [x] in the ceinter of the qs alg.
-- a slightly optimized version of qs which halves the work done. should try to test it
:{
let qs [] = []
qs (x:xs) = (qs front) ++ [x] ++ (qs back)
where front = hLGE x xs
back = thLGE x xs
:}
qs [6.3,9.1,4,1,5,3,2]
:{
let qs (x:xs) = (qs front) ++ (qs back)
where front = hLGE x xs
back = thLGE x xs
:}
z 3 4
hLGE 3 [9,4,1,5,3,2]
hLaGoE 3 [9,4,1,5,3,2] [] []
head $ tail [[1,5,3,2],[1,5,3,2,4]]
-- really should read lessAndGreaterOrEqualTo
:{
let lessAndGreater c [] a b = [a,b]
lessAndGreater c (x:xs) a b
| c > x = lessAndGreater c xs (x:a) b
| otherwise = lessAndGreater c xs a (x:b)
:}
lessAndGreater 3 [9,4,1,5,2] [] []
filter (<3) [1..5]
filter (<1) [2..5]
filter (<3) [4..5]
:{
let minList [] = x
minList [x] = x
minList (x:xs) = minList y
where y = filter (<x) xs
:}
:{
let minList [x] = x
minList (x:xs)
| x < y = x
| otherwise = y
where y = minList xs
:}
Just 3
Just [1..45]
minList [111,3,4,2,3]
minList [1,3,2,-1]
[50..1]
[3*x | x <- [1..4]]
[(51-x) | x <- [1..50]]
let n21 n = [((n+1)-x) | x <- [1..(n)]]
n21 40
min
import System.Random
:{
let randomList :: Int -> IO([Int])
randomList 0 = return []
randomList n = do
r <- randomRIO (1,6)
rs <- randomList (n-1)
return (r:rs)
:}
let a = randomList 34
a
minList a
data Person = Person String String Int Float String String deriving (Show)
let mike = Person "asdf" "fjd" 3 3 "asdf" "fjd"
:{
let mike2 :: Person
mike2 = Person "asdf" "fjd" 3 3.3 "asdf" "fjd"
:}
mike2
-- data is used to define a new type
data Nat = Z | S Nat deriving (Show)
data Ll a = Empty | Cons a (Ll a) deriving (Show)
:{
let ei :: Ll Int
ei = Empty
:}
:{
let ei :: Ll Int
ei = Cons 3 (Cons 3 Empty)
:}
:{
let mapL _ Empty = Empty
mapL f (Cons x xs) = Cons (f x) (mapL f xs)
:}
mapL (+1) fd
let fd = Cons 4 (Cons 3 Empty)
fd
let sd = Cons fd Empty
:t fd
:t sd
let ke = Cons 3 fd
cantor2 0
cantor2 3
:{
let cdr :: Ll a -> Ll a
cdr (Cons _ xs) = xs
:}
cdr (Cons 3 (Cons 2 Empty))
car $ cdr (Cons 3 (Cons 2 Empty))
let cadr = car . cdr
cadr (Cons 3 (Cons 2 Empty))
head $ tail [3,2]
data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving (Show)
-- have a data point at the node as well
data RTree a = Leaf a | Branch a (RTree a) (RTree a) deriving (Show)
:{
let leftT :: RTree a -> RTree a
leftT (Branch _ t1 t2) = t1
:}
:{
let rightT :: RTree a -> RTree a
rightT (Branch _ t1 t2) = t2
:}
:{
let car :: Ll a -> a
car (Cons x _) = x
:}
:{
let a :: RTree Int
a = Branch 3 (Branch 4 (Leaf 5) (Leaf 6)) (Branch 6 (Branch 7 (Leaf 32) (Leaf 3)) (Branch 89 (Leaf 32) (Leaf 3)))
:}
a
let b = Branch 3 a a
let c = Branch 4 b b
c
let d = Branch 1 (Branch (Leaf 2) (Leaf 3)) (Branch 4 (Leaf 34) (Leaf 5))
let d = Branch 1 (Leaf 3) (Leaf 4)
let e = Branch 1 d d
e
let f = Branch 3 (Leaf 3) e
-- shouldn't work because, we can't likewise test "isTree"
:{
let isLeaf :: RTree Int -> Bool
isLeaf t
t == Leaf a = True
otherwise = False
:}
f
b
leftT a
leftT $ leftT $ leftT a
head []
rightT a
{-# LANGUAGE DeriveFunctor #-}
data Ree a = Leaf a | Branch a (Ree a) (Ree a) deriving (Functor, Show)
:{
let lTree :: RTree [Int]
lTree = Branch [1,2,3] (Leaf [1..4]) (Leaf [2..5])
:}
let lTree1 = Branch [5,10] lTree lTree
mapRTree (map (+1)) lTree1
:{
let tree1 :: Tree Int
tree1 = Branch (Leaf 3) (Leaf 4)
:}
:{
let tree1 :: RTree Int
tree1 = Branch 5 (Leaf 3) (Leaf 4)
:}
tree1
let tree2 = Branch 6 tree1 tree1
tree2
let tree2' = mapRTree (*3) tree2
let tree3 = Branch 19 tree2 tree2'
tree3
mapTree (*3) tree2
:{
let mapRTree f (Leaf a) = Leaf (f a)
mapRTree f (Branch n t1 t2) = Branch (f n) (go t1) (go t2)
where go = mapRTree f
:}
:{
let mapTree f (Leaf a) = Leaf (f a)
mapTree f (Branch t1 t2) = Branch (go t1) (go t2)
where go = mapTree f
:}
cdr Empty
:{
let car :: Ll a -> a
car (Cons x _) = x
:}
let b = errorEmptyList "head"
errorWithoutStackTrace :: forall (r :: RuntimeRep). forall (a :: TYPE r).
[Char] -> a
:{
let errorWithoutStackTrace s =
let ?callStack = freezeCallStack emptyCallStack
in error s
:}
:{
let errorEmptyList :: String -> a
errorEmptyList fun =
errorWithoutStackTrace (prel_list_str ++ fun ++ ": empty list")
:}
:t errorWithoutStackTrace
:t error
:{
let prel_list_str :: String
prel_list_str = "Prelude."
:}
head []
tail []
tail [s
:t Z
:t (S Z)
:{
let qwer :: Nat
qwer = S Z
:}
:t (<)
data Ord
sdf = S Z
:{
let rqwer :: Nat
rqwer = S (S Z)
:}
qwer
rqwer
natPlus qwer rqwer
natPlus qwer Z
natPlus Z Z
data Nat = Z | S Nat deriving (Show)
:{
let natPlus a Z = a
natPlus Z a = a
natPlus (S a) (S b) = natPlus a (S (S b))
:}
:t natPlus
:{
let natPlus Z a = a
natPlus (S a) b = natPlus a (S b)
:}
:{
let natPlus2 a Z = a
natPlus2 (S a) (S b) = natPlus2 (S (S a)) b
:}
natPlus (S Z) (S (S Z))
natPlus2 (S Z) (S (S Z))
natMult (S Z) (S (S Z))
natMult (S (S Z)) (S (S Z))
:{
let natMult Z a = Z
natMult (S a) (S b) = natPlus (S b) (natMult a (S b))
:}
:{
let natMult :: Nat -> Nat -> Nat
natMult Z a = Z
natMult (S a) b = natPlus b (natMult a b)
:}
-- for whatever reason this is not commutative
-- finally correct
:{
let natMult :: Nat -> Nat -> Nat
natMult Z a = Z
natMult (S a) b = natPlus (natMult a b) b
:}
:{
let natMult :: Nat -> Nat -> Nat
natMult Z a = Z
natMult (S a) b = natPlus b (natMult a b)
:}
-- finally correct
:{
let natMult :: Nat -> Nat -> Nat
natMult a Z = Z
natMult a (S b) = natPlus (natMult a b) a
:}
:t natMult
natMult (S (S Z)) (S (S Z))
natMult (S (S (S Z))) (S (S Z))
natMult (S (S (S Z))) Z
natMult (S Z) (S (S (S Z)))
natMult (S (S (S Z))) (S Z)
natPlus (S (S (S Z))) (S Z)
natPlus (S Z) (S Z)
natPlus (S (S Z)) (S (S Z))
:{
let lt :: Nat -> Nat -> Bool
lt Z (S b) = True
lt (S a) (S b) = lt a b
:}
:{
let isPos x
| x > 0 = True
| otherwise = False
:}
map isPos [-2,-1,0,1,4]
:t 3
gt a b
| (a-b) > 0 = True
| otherwise = False
:{
let min a b
| a <= b = a
| otherwise = b
:}
min (-44) 4.1
foldl (+) 0 [1..4]
foldl min 99 [1..4]
foldl min 99 [1..4]
-- so to define minL via a fold requires a priori knowledge of some kind of upper bound to the minimum
let minL = foldl min 999999
minL [1,3,4,(-23),390321]
:{
let nto1 0 = []
nto1 n = n : nto1 (n-1)
:}
:t nto1
nto1 50
:{
let isLowerBound x [] = True
isLowerBound x (y:ys)
| x <= y = isLowerBound x ys
| otherwise = False
:}
isLowerBound 1.1 [1..5]
isLowerBound 8 [1..5]
isMin 3 [1..5]
isMin 8 [1..5]
minList [1..5]
| x < restof xs = x
| otherwise = minList xs
slowSort (x:xs)
| x < slowSort xs = x
| otherwise
I was trying to define natural number multiplication in Haskell, and kept getting the error below (corresponds to the second natMult definition below).
Prelude> natMult (S (S Z)) (S (S Z))
*** Exception: <interactive>:(4,5)-(5,45): Non-exhaustive patterns in function natPlus
Here's my code:
data Nat = Z | S Nat deriving (Show)
natPlus :: Nat -> Nat -> Nat
natPlus Z a = a
natPlus (S a) (S b) = natPlus a (S (S b))
After a bit of tinkering, I realized this definition works fine, whereas the second one below is broken. The only difference is the order of the input parameters for natPlus.
-- works fine
natMult :: Nat -> Nat -> Nat
natMult Z a = Z
natMult (S a) b = natPlus (natMult a b) b
-- gives gives the error above
natMult :: Nat -> Nat -> Nat
natMult Z a = Z
natMult (S a) b = natPlus b (natMult a b)
:{
let natPlus Z a = a
natPlus (S a) b = natPlus a (S b)
:}