-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter4.hs
816 lines (594 loc) · 20.3 KB
/
Chapter4.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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
-- | Chapter 4 - Desinging ans writing programs
module Chapter4 where
import Test.QuickCheck (quickCheck)
import Test.QuickCheck.Arbitrary
import Test.QuickCheck.Gen
import Test.HUnit
import PicturesSVG
maxThree :: Integer -> Integer -> Integer -> Integer
maxThree x y z = max ( max x y) z
-- testing section 4.8
testMaxl = TestCase (assertEqual "for: maxThree 6 4 1" 6 (maxThree 6 4 1))
testMax2 = TestCase (assertEqual "for: maxThree 6 6 6" 6 (maxThree 6 6 6))
testMax3 = TestCase (assertEqual "for: maxThree 2 6 6" 6 (maxThree 2 6 6))
testMax4 = TestCase (assertEqual "for: maxThree 2 2 6" 6 (maxThree 2 2 6))
testMax5 = TestCase (assertEqual "for: MaxThree 6 6 2" 6 (maxThree 6 6 2))
testsMax = TestList [testMaxl, testMax2, testMax3, testMax4, testMax5]
-- 4.1 Middle number example
middleNumber :: Integer -> Integer -> Integer -> Integer
middleNumber x y z
| between y x z = x
| between x y z = y
| otherwise = z
between :: Integer -> Integer -> Integer -> Bool
between x y z =
(x >= y && y >= z) || (x <= y && y <= z)
-- Exercises
-- Ex 4.1 define maxFour
maxFour :: Integer -> Integer -> Integer -> Integer -> Integer
maxFour w x y z
| w >= x && w >= y && w >= z = w
| x >= y && x >= z = x
| y >= z = y
| otherwise = z
maxFour1 :: Integer -> Integer -> Integer -> Integer -> Integer
maxFour1 w x y z = max (max (max w x) y) z
maxFour2 :: Integer -> Integer -> Integer -> Integer -> Integer
maxFour2 w x y z = max (maxThree w x y) z
prop_maxFour :: Integer -> Integer -> Integer -> Integer -> Bool
prop_maxFour w x y z =
maxFour w x y z == maxFour1 w x y z
prop_maxFour1 :: Integer -> Integer -> Integer -> Integer -> Bool
prop_maxFour1 w x y z =
maxFour w x y z == maxFour2 w x y z
-- Ex 4.2 middle number using weak asending order
middleNumber1 :: Integer -> Integer -> Integer -> Integer
middleNumber1 x y z
| weakAscendingOrder x y z || weakAscendingOrder z y x = y
| weakAscendingOrder y x z || weakAscendingOrder z x y = x
| otherwise = z
weakAscendingOrder :: Integer -> Integer -> Integer -> Bool
weakAscendingOrder m n p = (p >= n) && (n >= m)
prop_middleNumber :: Integer -> Integer -> Integer -> Bool
prop_middleNumber x y z =
middleNumber x y z == middleNumber1 x y z
-- Ex 4.3 how many equal
-- From section 3.2
threeEqual :: Integer -> Integer -> Integer -> Bool
threeEqual m n p = (m==n) && (n==p)
-- from Ex 3.9
threeDifferent :: Integer -> Integer -> Integer -> Bool
threeDifferent m n p = (m /= n) && (n /= p) && (m /= p)
howManyEqual :: Integer -> Integer -> Integer -> Integer
howManyEqual x y z
| threeEqual x y z = 3
| threeDifferent x y z = 1
| otherwise = 2
-- Ex 4.4 - How many equal four
howManyEqual4 :: Integer -> Integer -> Integer -> Integer -> Integer
howManyEqual4 w x y z
| fourEqual w x y z = 4
| onlyThreeEqual w x y z = 3
| fourDifferent w x y z = 1
| otherwise = 2
fourEqual :: Integer -> Integer -> Integer -> Integer -> Bool
fourEqual w x y z =
threeEqual w x y && w == z
onlyThreeEqual :: Integer -> Integer -> Integer -> Integer -> Bool
onlyThreeEqual w x y z =
threeEqual w x y || threeEqual w x z || threeEqual w y z || threeEqual x y z
fourDifferent :: Integer -> Integer -> Integer -> Integer -> Bool
fourDifferent w x y z =
threeDifferent w x y && z /= w && z /= x && z /= y
-- 4.2 Solving a problem ins steps: local definitions
fourPics0 :: Picture -> Picture
fourPics0 pic =
left `beside` right
where
left = pic `above` invertColour pic
right = invertColour (flipV pic) `above` flipV pic
fourPics1 :: Picture -> Picture
fourPics1 pic =
left `beside` right
where
left = pic `above` invertColour pic
right = invertColour flipped `above` flipped
flipped = flipV pic
fourPics2 :: Picture -> Picture
fourPics2 pic =
left `beside` right
where
left = pic `above` invertColour pic
right = invertColour (flipV left)
fourPics3 :: Picture -> Picture
fourPics3 pic =
left `beside` right
where
stack p = p `above` invertColour p
left = stack pic
right = stack (invertColour (flipV pic))
-- area of trinangle
triArea :: Float -> Float -> Float -> Float
triArea a b c
| possible = sqrt(s*(s-a)*(s-b)*(s-c))
| otherwise = 0
where
s = (a+b+c)/2
possible = greaterThanZero && lengthOfSide
where
greaterThanZero = (a > 0) && (b > 0) && (c > 0)
lengthOfSide = (a < b+c) && (b < a+c) && (c < a+b)
sumSquares :: Integer -> Integer -> Integer
sumSquares n m
= sqN + sqM
where
sqN = n*n
sqM = m*m
-- Scopes
isOdd, isEven :: Int -> Bool
isOdd n
| n<=0 = False
| otherwise = isEven (n-1)
isEven n
| n<0 = False
| n==0 = True
| otherwise = isOdd (n-1)
maxsq :: Int -> Int -> Int
maxsq x y
| sqx > sqy = sqx
| otherwise = sqy
where
sqx = sq x
sqy = sq y
sq :: Int -> Int
sq z = z*x
-- Exercises
-- Ex 4.5 yet two more ways to define fourPics
-- Ex 4.6
{-
fourPics :: Picture -> Picture
fourPics pic =
top `above` bottom
where
top = ...:
bottom ...
-}
fourPics4 :: Picture -> Picture
fourPics4 pic =
top `above` bottom
where
top = pic `beside` invertColour (flipV pic)
bottom = invertColour pic `beside` flipV pic
fourPics5 :: Picture -> Picture
fourPics5 pic =
top `above` bottom
where
top = pic `beside` invertColour flipped
bottom = invertColour pic `beside` flipped
flipped = flipV pic
fourPics6 :: Picture -> Picture
fourPics6 pic =
top `above` bottom
where
top = pic `beside` invertColour (flipV pic)
bottom = invertColour top
-- Exercises
-- Ex 4.7
-- TODO:
-- Ex 4.8 Define prossible values -- see above.
possible :: Integer -> Integer -> Integer -> Bool
possible a b c
= greaterThanZero && lengthOfSide
where
greaterThanZero = (a > 0) && (b > 0) && (c > 0)
lengthOfSide = (a < b+c) && (b < a+c) && (c < a+b)
-- Ex 4.9 - define maxThreeOccurs
maxThreeOccurs :: Int -> Int -> Int -> (Int,Int)
maxThreeOccurs m n o =
(max3e, numOccurs max3e)
where
max3e = max m (max n o)
numOccurs mx
| allFourEqual mx = 3
| anyTwoEqual mx = 2
| otherwise = 1
where
allFourEqual w =
(w==m) && (w==n) && (w==o)
anyTwoEqual w =
threeEqual w m n || threeEqual w n o || threeEqual w m o
threeEqual w x y = (w==x) && (w==y)
-- $.3 Defining types for ourselves: enumerated types.
-- Rock Paper Sissors
data Move = Rock | Paper | Scissors
deriving (Eq)
-- Showing Moves in an abbreviated form.
instance Show Move where
show Rock = "r"
show Paper = "p"
show Scissors = "s"
-- For QuickCheck to work over the Move type.
instance Arbitrary Move where
arbitrary = elements [Rock, Paper, Scissors]
-- Calculating the Move to beat or lose against the
-- argument Move.
beat,lose :: Move -> Move
beat Rock = Paper
beat Paper = Scissors
beat Scissors = Rock
lose Rock = Scissors
lose Paper = Rock
lose Scissors = Paper
-- Exercises
-- Ex 4.11 Result datatype
data Result = Win | Lose | Draw
deriving (Show,Eq)
-- Ex 4.12 outcme function
outcome :: Move -> Move -> Result
outcome mv1 mv2
| mv1 == beat mv2 = Win
| mv1 == lose mv2 = Lose
| otherwise = Draw
-- Ex 4.13 a Win vs Lose property
-- win . loose and lose . win should be an ID funtion one move
prop_winLose :: Move -> Bool
prop_winLose move =
beat (lose move) == move
-- beat (lose move) == lose (beat move)
-- Ex 4.14 quickCheck property to test outcome
-- if move1 /= move2 then (move1 beat move2) == (move2 lose move1)
prop_outcome :: Move -> Move -> Bool
prop_outcome mv1 mv2
| outcome mv1 mv2 == Win = outcome mv2 mv1 == Lose
| outcome mv1 mv2 == Lose = outcome mv2 mv1 == Win
| outcome mv1 mv2 == Draw = outcome mv2 mv1 == Draw
| otherwise = False
-- is this a tautology?
-- Standard Types
-- Exercises
-- Ex 4.15 Season type and function
data Season = Spring | Summer | Fall | Winter
deriving (Eq,Show,Ord)
data Temp = Cold | Hot
deriving (Eq,Show,Ord)
temperature :: Season -> Temp
temperature sz
| sz == Spring = Cold
| sz == Summer = Hot
| sz == Fall = Hot
| otherwise = Cold
-- Ex 4.16 month data type and function
data Month = January | February | March |
April | May | June |
July | August | September |
October | November | December
deriving (Eq, Show, Ord)
monthToSeason :: Month -> Season
monthToSeason mth
| mth == March || mth == April || mth == May = Spring
| mth == June || mth == July || mth == August = Summer
| mth == September || mth == October || mth == November = Fall
| otherwise = Winter
-- 4.4 Recursion -- Oh, boy!
-- Factorials
fac :: Integer -> Integer
fac n
| n==0 = 1
| n>0 = fac (n-1) * n
| otherwise = error "'fac' only defined on natural numbers"
-- Exercises
-- Ex 4.17 RangeProduct
rangeProduct :: Integer -> Integer -> Integer
rangeProduct n m
| m == n = n
| m > n = m * rangeProduct n (m-1)
| otherwise = 0
--- Ex 4.18 fac implemented with rangeProduct
fac1 :: Integer -> Integer
fac1 n = rangeProduct 1 n
{- --templase
fun :: Integer -> a
fun n
| n==0 = _fun1
| n>0 = _fun2 (fun (n-1))
-}
-- Example:
-- powers of 2
power2 :: Integer -> Integer
power2 n
| n==0 = 1
| n>0 = 2 * power2 (n-1)
-- sum of factorials
sumFacs :: Integer -> Integer
sumFacs n
| n==0 = 1
| n>0 = sumFacs (n-1) + fac n
-- sum of some functions f
sumFun :: (Integer -> Integer) -> Integer -> Integer
sumFun f n
| n==0 = f 0
| n>0 = sumFun f (n-1) + f n
sumFacs' :: Integer -> Integer
sumFacs' n = sumFun fac n
-- number of regions formed by cuts:
regions :: Integer -> Integer
regions n
| n==0 = 1
| n>0 = regions (n-1) + n
-- Exercises:
-- Ex 4.19 - define multiplication
mult :: Integer -> Integer -> Integer
mult n m
| m==0 = 0
| n>0 = n + mult n (m-1)
-- Ex 4.20 integer square root
intSqrRoot :: Integer -> Integer
intSqrRoot n = loop n n
where
loop n m
| n*n <= m = n
| otherwise = loop (n-1) m
-- Ex 4.21 maximum of functions calls
maxFun :: (Integer -> Integer) -> Integer -> Integer
maxFun f n
| n==0 = f 0
| n>0 = max (f n) (maxFun f (n-1))
-- a testing function - maxFun fn 100 => 44
fn :: Integer -> Integer
fn 0 = 0
fn 1 = 44
fn 2 = 17
fn _ = 0
-- Ex 4.22 function that takes a funtion and returns true
-- if given funtion returns 0
fun422 :: (Integer -> Integer) -> Integer -> Bool
fun422 f n
| n==0 = False
| n>0 = if f n == 0
then True
else fun422 f (n-1)
-- Ex 4.23 define regions in terms of sumFun
regions2 :: Integer -> Integer
regions2 n = sumFun id' n + 1
where id' a = a
--regions2 n = sumFun (+0) n + 1
-- Ex 4.24 3d regions - cake cuting algorithm
regions3d :: Integer -> Integer
regions3d n
| n==0 = 1
| n>0 = regions3d (n-1) + regions (n-1)
-- $.6 pictures
blackSquares :: Integer -> Picture
blackSquares n
| n<=1 = black
| otherwise = black `beside` blackSquares (n-1)
whiteSquares :: Integer -> Picture
whiteSquares n
| n<=1 = white
| otherwise = white `beside` whiteSquares (n-1)
blackWhite :: Integer -> Picture
blackWhite n
| n<=1 = black
| otherwise = black `beside` whiteBlack (n-1)
whiteBlack :: Integer -> Picture
whiteBlack n
| n<=1 = white
| otherwise = white `beside` blackWhite (n-1)
blackChess :: Integer -> Integer -> Picture
blackChess n m
| n<=1 = blackWhite m
| otherwise = blackWhite m `above` whiteChess (n-1) m
whiteChess :: Integer -> Integer -> Picture
whiteChess n m
| n<=1 = whiteBlack m
| otherwise = whiteBlack m `above` blackChess (n-1) m
chess :: Integer -> Picture
chess n = blackChess n n
-- Exercises
-- 4.25 - complete whiteBlack whiteChess - Done
-- 4.36 column of pictures
column :: Picture -> Integer -> Picture
column pic n
| n<=1 = pic
| otherwise = pic `above` column pic (n-1)
-- 4.27 black diagonal
blackDiagonal :: Integer -> Picture
blackDiagonal num = loop num num
where
loop n k
| n<=1 = rowBW k 1
| otherwise = rowBW k n `above` loop (n-1) k
rowBW :: Integer -> Integer -> Picture
rowBW n m
| n<=1 = blackOrWhite m 1
| otherwise = blackOrWhite m n `beside` rowBW (n-1) m
where
blackOrWhite m n
| n==m = black
| otherwise = white
-- Ex 4.28 reverse black diagonl
revBlackDiagonal :: Integer -> Picture
revBlackDiagonal num = loop num num
where
loop n k
| n<=1 = rowBW k k
| otherwise = rowBW k (k-n+1) `above` loop (n-1) k
-- Ex 4.28 -- double diagonal
doubleDiagonal :: Integer -> Picture
doubleDiagonal num = loop num num
where
loop n k
| n<=1 = rowBW2 k 1 k
| otherwise = rowBW2 k n (k-n+1) `above` loop (n-1) k
rowBW2 :: Integer -> Integer -> Integer -> Picture
rowBW2 n m o
| n<=1 = blackOrWhite 1 m o
| otherwise = blackOrWhite n m o `beside` rowBW2 (n-1) m o
where
blackOrWhite n m o
| n==m = black
| n==o = black
| otherwise = white
-- Ex 4.30 - direct recursive chessBoard
chessBoard :: Integer -> Picture
chessBoard num = loop num num
where
loop n k
| n<=1 = pickRow 1
| otherwise = pickRow n `above` loop (n-1) k
where
pickRow n1
| (n1 `mod` 2) == 0 = blackWhite k
| otherwise = whiteBlack k
-- 4.7 General forms of recursion
-- Examples
-- Fibonacci
fib :: Integer -> Integer
fib n
| n==0 = 0
| n==1 = 1
| n>1 = fib (n-2) + fib (n-1)
-- remander and divide
remainder :: Integer -> Integer -> Integer
divide :: Integer -> Integer -> Integer
remainder m n
| m<n = m
| otherwise = remainder (m-n) n
divide m n
| m<n = 0
| otherwise = 1 + divide (m-n) n
factorInRange :: Integer -> Integer -> Bool
factorInRange k n
| k >= n = False
| mod n k == 0 = True
| otherwise = factorInRange (k+1) n
prime :: Integer -> Bool
prime n = (n>1) && not (factorInRange 2 n)
-- Exercises
-- Ex 4.31 greatest common factor
-- Euclids algorithm
gcd' :: Integer -> Integer -> Integer
gcd' m n
| mod m n == 0 = n
| otherwise = gcd' n (mod m n)
-- gcd' 468 24 => 12
-- gcd' 135 19 => 1
-- Dijkstra's Algorithm
gcd2 :: Integer -> Integer -> Integer
gcd2 m n
| m==n = m
| m>n = gcd2 (m-n) n
| otherwise = gcd2 m (n-m)
-- Ex 4.32 - power of two
powerOfTwo :: Integer -> Integer
powerOfTwo n
| n==0 = 1
| n==1 = 2
| mod n 2 == 0 = powerOfTwo (div n 2) *
powerOfTwo (div n 2)
| otherwise = 2 * powerOfTwo (div n 2) *
powerOfTwo (div n 2)
-- 4.8 Program testing
mysteryMax :: Integer -> Integer -> Integer -> Integer
mysteryMax x y z
| x > y && x > z =x
| y > x && y > z =y
| otherwise = z
testMysteryMaxl = TestCase (assertEqual "for: mysteryMax 6 4 1" 6 (mysteryMax 6 4 1))
testMysteryMax2 = TestCase (assertEqual "for: mysteryMax 6 6 6" 6 (mysteryMax 6 6 6))
testMysteryMax3 = TestCase (assertEqual "for: mysteryMax 2 6 6" 6 (mysteryMax 2 6 6))
testMysteryMax4 = TestCase (assertEqual "for: mysteryMax 2 2 6" 6 (mysteryMax 2 2 6))
testMysteryMax5 = TestCase (assertEqual "for: mysteryMax 6 6 2" 6 (mysteryMax 6 6 2))
testsMysterMax = TestList [testMysteryMaxl, testMysteryMax2, testMysteryMax3,
testMysteryMax4, testMysteryMax5]
fact :: Int -> Int
fact n
| n>1 = n * fact (n-1)
| otherwise = 1
-- fails on 17
prop_fact n =
fact n > 0
-- Exercises
-- 4.32 test allEqual
allEqual :: Integer -> Integer -> Integer -> Bool
allEqual m n p = (m==n) && (n==p)
testAllEqual1 = TestCase (assertEqual "allEqual 5 5 5" True (allEqual 5 5 5))
testAllEqual2 = TestCase (assertEqual "allEqual 5 5 3" False (allEqual 5 5 3))
testAllEqual3 = TestCase (assertEqual "allEqual 3 5 3" False (allEqual 3 5 3))
testAllEqual4 = TestCase (assertEqual "allEqual 3 5 5" False (allEqual 3 5 5))
testAllEqual5 = TestCase (assertEqual "allEqual 2 3 5" False (allEqual 2 3 5))
testAllEqual6 = TestCase (assertEqual "allEqual -5 5 0" False (allEqual (-5) 5 0))
testsAllEqual = TestList [testAllEqual1 , testAllEqual2, testAllEqual3, testAllEqual4,
testAllEqual5, testAllEqual6]
-- quickCheck -- compare against "trusted" threeEqual
prop_allEqual m n p =
allEqual m n p == threeEqual m n p
-- allEqual should be different for allDifferent:
prop_allEqual_not_threeDifferent m n p =
not (allEqual m n p && threeDifferent m n p)
-- Ex 4.34
solution :: Integer -> Integer -> Integer -> Bool
solution m n p = ((m+n+p)==3*p)
-- pretend allEqual
testSolution1 = TestCase (assertEqual "solution1 5 5 5" True (solution 5 5 5))
testSolution2 = TestCase (assertEqual "solution1 5 5 3" False (solution 5 5 3))
testSolution3 = TestCase (assertEqual "solution1 3 5 3" False (solution 3 5 3))
testSolution4 = TestCase (assertEqual "solution1 3 5 5" False (solution 3 5 5))
testSolution5 = TestCase (assertEqual "solution1 2 3 5" False (solution 2 3 5))
-- this should fail:
testSolution6 = TestCase (assertEqual "solution1 -5 5 0" False (solution (-5) 5 0))
testsAllSolution = TestList [testSolution1 , testSolution2, testSolution3, testSolution4,
testSolution5, testSolution6]
-- all tests passed, except when any are negative
-- quicCheck against "trusted" threeEqual -- this should fail
prop_solution :: Integer -> Integer -> Integer -> Bool
prop_solution m n o =
solution m n o == threeEqual m n o
-- failed: -4 6 1, 5 -1 2, 2 -6 -2, -12 2 -5, 3 7 5,
-- but many runs of quickCheck passed, oops
-- Ex 4.35 test all different
allDifferent :: Integer -> Integer -> Integer -> Bool
allDifferent m n p = (m /= n) && (n /= p) && (m /= p)
testAllDifferent1 = TestCase (assertEqual "allDifferent 5 5 5" False (allDifferent 5 5 5))
testAllDifferent2 = TestCase (assertEqual "allDifferent 5 5 3" False (allDifferent 5 5 3))
testAllDifferent3 = TestCase (assertEqual "allDifferent 3 5 3" False (allDifferent 3 5 3))
testAllDifferent4 = TestCase (assertEqual "allDifferent 3 5 5" False (allDifferent 3 5 5))
testAllDifferent5 = TestCase (assertEqual "allDifferent 2 3 5" True (allDifferent 2 3 5))
testAllDifferent6 = TestCase (assertEqual "allDifferent -5 5 0" True (allDifferent (-5) 5 0))
testsAllDifferent = TestList [testAllDifferent1 , testAllDifferent2, testAllDifferent3,
testAllDifferent4, testAllDifferent5, testAllDifferent6]
-- quickCheck - test against "trusted" threeDifferent
prop_allDifferent :: Integer -> Integer -> Integer -> Bool
prop_allDifferent m n o =
allDifferent m n o == threeDifferent m n o
-- Ex 4.36 test attempt
attempt :: Integer -> Integer -> Integer -> Bool
attempt m n p = (m/=n) && (n/=p)
testAttempt1 = TestCase (assertEqual "attempt 5 5 5" False (attempt 5 5 5))
testAttempt2 = TestCase (assertEqual "attempt 5 5 3" False (attempt 5 5 3))
testAttempt3 = TestCase (assertEqual "attempt 3 5 3" False (attempt 3 5 3)) -- fails
testAttempt4 = TestCase (assertEqual "attempt 3 5 5" False (attempt 3 5 5))
testAttempt5 = TestCase (assertEqual "attempt 2 3 5" True (attempt 2 3 5))
testAttempt6 = TestCase (assertEqual "attempt -5 5 0" True (attempt (-5) 5 0))
testsAttempt = TestList [testAttempt1 , testAttempt2, testAttempt3, testAttempt4,
testAttempt5, testAttempt6]
-- Faulty function, it doesn't catch the 3 5 3 case since it doesn't test m /= p case
-- Ex 4.38 - test power of two: from ex 4.32
powerOfTwo' :: Integer -> Integer
powerOfTwo' n
| n==0 = 1
| n==1 = 2
| mod n 2 == 0 = powerOfTwo' (div n 2) *
powerOfTwo' (div n 2)
| otherwise = 2 * powerOfTwo (div n 2) *
powerOfTwo' (div n 2)
testPowerOfTwo1 = TestCase (assertEqual "powerOfTwo' 0" 1 (powerOfTwo' 0))
testPowerOfTwo2 = TestCase (assertEqual "powerOfTwo' 1" 2 (powerOfTwo' 1))
testPowerOfTwo3 = TestCase (assertEqual "powerOfTwo' 2" 4 (powerOfTwo' 2))
testPowerOfTwo4 = TestCase (assertEqual "powerOfTwo' 3" 8 (powerOfTwo' 3))
testPowerOfTwo5 = TestCase (assertEqual "powerOfTwo' 4" 16 (powerOfTwo' 4))
testsPowerOfTwo = TestList [testPowerOfTwo1, testPowerOfTwo2, testPowerOfTwo3, testPowerOfTwo4, testPowerOfTwo5]
-- don't run.
--prop_powerOfTwo n =
-- powerOfTwo n == 2^n
-- Ex 4.49 Write QuickCheck test for the exercises: See above.