-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chapter4.hs
367 lines (259 loc) · 8.21 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
--------------------------------------------------------------------------
--
-- Haskell: The Craft of Functional Programming, 3e
-- Simon Thompson
-- (c) Addison-Wesley, 1996-2011.
--
-- Chapter 4
--
--------------------------------------------------------------------------
-- NOTE
--
-- Added HUnit and QuickCheck tests
--
-- HUnit 1.0 documentation is out of date
-- re package name.
module Chapter4 where
import PicturesSVG hiding (test2)
import Test.HUnit
import Test.QuickCheck
-- Designing a program in Haskell
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
maxThree :: Int -> Int -> Int -> Int
maxThree x y z = (x `max` y) `max` z
testMax1 = 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))
-- run as
-- runTestTT testsMax
testsMax = TestList [testMax1, testMax2, testMax3, testMax4]
-- NOTE
--
-- Added this type synonym so that can switch easily
-- between Integer and Int.
type MyNum = Integer
middleNumber :: MyNum -> MyNum -> MyNum -> MyNum
middleNumber x y z
| between y x z = x
| between x y z = y
| otherwise = z
-- What follows here is a dummy definition of between; you need to replace this
-- with a proper definition for the function middleNumber to work.
between :: MyNum -> MyNum -> MyNum -> Bool
-- dummy definition
-- for you to complete!
between = between
-- NOTE
--
-- HUnit tests added
--
-- To run evaluate: runTestTT tests
test1 = TestCase (assertEqual "for: between 2 3 4" True (between 2 3 4))
test2 = TestCase (assertEqual "for: between 2 3 2" False (between 2 3 2))
test3 = TestCase (assertEqual "for: between 2 3 3" True (between 2 3 3))
test4 = TestCase (assertEqual "for: between 3 3 3" True (between 3 3 3))
test5 = TestCase (assertEqual "for: between 3 2 3" False (between 3 2 3))
test6 = TestCase (assertEqual "for: between 3 2 1" True (between 3 2 1))
testsBetween = TestList [test1, test2, test3, test4, test5, test6]
-- NOTE
--
-- Interesting to vary the implementation and see which tests fail.
-- Simple form of mutation testing.
-- QuickCheck test
--
-- Does the tricky implementation of between work in the
-- same way as the case analysis?
prop_between :: MyNum -> MyNum -> MyNum -> Bool
prop_between x y z
= between x y z == ((x<=y)&&(y<=z))||((x>=y)&&(y>=z))
-- Unit tests as Quick Check properties
prop_between1 :: Bool
prop_between1
= between 2 3 4 == True
-- Local definitions
-- ^^^^^^^^^^^^^^^^^
-- Four ways of defining a Picture using
-- different combinations of loca definitions.
fourPics1 :: Picture -> Picture
fourPics1 pic =
left `beside` right
where
left = pic `above` invertColour pic
right = invertColour (flipV pic) `above` flipV pic
fourPics2 :: Picture -> Picture
fourPics2 pic =
left `beside` right
where
left = pic `above` invertColour pic
right = invertColour flipped `above` flipped
flipped = flipV pic
fourPics3 :: Picture -> Picture
fourPics3 pic =
left `beside` right
where
left = pic `above` invertColour pic
right = invertColour (flipV left)
fourPics4 :: Picture -> Picture
fourPics4 pic =
left `beside` right
where
stack p = p `above` invertColour p
left = stack pic
right = stack (invertColour (flipV pic))
-- Area of a triangle
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 = possible -- dummy definition
-- Sum of squares
sumSquares :: Integer -> Integer -> Integer
sumSquares n m
= sqN + sqM
where
sqN = n*n
sqM = m*m
-- Let expressions
-- ^^^^^^^^^^^^^^^
-- Two examples which use `let'.
letEx1 :: Integer
letEx1 = let x = 3+2 in x^2 + 2*x - 4
letEx2 :: Integer
letEx2 = let x = 3+2 ; y = 5-1 in x^2 + 2*x - y
-- 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)
-- Defining types for ourselves
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- Rock - Paper - Scissors
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
-- Primitive recursion over Int
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- The factorial of n is 1*2*...*(n-1)*n, so that factorial of four is 24.
-- It is often written n!
fac :: Integer -> Integer
fac n
| n==0 = 1
| n>0 = fac (n-1) * n
| otherwise = error "fac only defined on natural numbers"
-- n
-- Raising two to a power: power2 n is 2 in mathematical notation.
power2 :: Integer -> Integer
power2 n
| n==0 = 1
| n>0 = 2 * power2 (n-1)
-- The sum of the factorials up to a particular value, 0! + 1! + ... n!.
sumFacs :: Integer -> Integer
sumFacs n
| n==0 = 1
| n>0 = sumFacs (n-1) + fac n
-- The sum of the values of a function up to a particular value:
-- f 0 + f 1 + ... f n
-- from which you can reconstruct sumFacs: sumFacs n = sumFun fac n
sumFun :: (Integer -> Integer) -> Integer -> Integer
sumFun f n
| n==0 = f 0
| n>0 = sumFun f (n-1) + f n
-- The maximum number of regions into which n lines can cut a plane.
regions :: Integer -> Integer
regions n
| n==0 = 1
| n>0 = regions (n-1) + n
-- The Fibonacci numbers 0, 1, 1, 2, 3, 5, ..., u, v, u+v, ...
fib :: Integer -> Integer
fib n
| n==0 = 0
| n==1 = 1
| n>1 = fib (n-2) + fib (n-1)
-- Division of integers
remainder :: Integer -> Integer -> Integer
remainder m n
| m<n = m
| otherwise = remainder (m-n) n
divide :: Integer -> Integer -> Integer
divide m n
| m<n = 0
| otherwise = 1 + divide (m-n) n
-- Testing
-- ^^^^^^^
-- Does this function calculate the maximum of three numbers?
mysteryMax :: Integer -> Integer -> Integer -> Integer
mysteryMax x y z
| x > y && x > z = x
| y > x && y > z = y
| otherwise = z
testMMax1 = TestCase (assertEqual "for: mysteryMax 6 4 1" 6 (mysteryMax 6 4 1))
testMMax2 = TestCase (assertEqual "for: mysteryMax 6 6 6" 6 (mysteryMax 6 6 6))
testMMax3 = TestCase (assertEqual "for: mysteryMax 2 6 6" 6 (mysteryMax 2 6 6))
testMMax4 = TestCase (assertEqual "for: mysteryMax 2 2 6" 6 (mysteryMax 2 2 6))
testMMax5 = TestCase (assertEqual "for: mysteryMax 6 6 2" 6 (mysteryMax 6 6 2))
testsMMax = TestList [testMMax1, testMMax2, testMMax3, testMMax4, testMMax5]
-- Numbers of roots
numberNDroots :: Float -> Float -> Float -> Integer
numberNDroots a b c
| bsq > fac = 2
| bsq == fac = 1
| bsq < fac = 0
where
bsq = b*b
fac = 4.0*a*c
-- Area of a triangle
triArea :: Float -> Float -> Float -> Float
triArea a b c
| possible a b c = sqrt(s*(s-a)*(s-b)*(s-c))
| otherwise = 0
where
s = (a+b+c)/2
possible :: Float -> Float -> Float -> Bool
possible a b c = True -- dummy definition
fact :: Int -> Int
fact n
| n>1 = n * fact (n-1)
| otherwise = 1
prop_fact n =
fact n > 0
-- Extended exercise
-- ^^^^^^^^^^^^^^^^^
blackSquares :: Integer -> Picture
blackSquares n
| n<=1 = black
| otherwise = black `beside` blackSquares (n-1)
blackWhite :: Integer -> Picture
blackWhite n
| n<=1 = black
| otherwise = black `beside` whiteBlack (n-1)
whiteBlack = error "exercise for you"
blackChess :: Integer -> Integer -> Picture
blackChess n m
| n<=1 = blackWhite m
| otherwise = blackWhite m `above` whiteChess (n-1) m
whiteChess n m = error "exercise for you"