-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter-14.hs
367 lines (268 loc) · 9.76 KB
/
chapter-14.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
-- Chapter 14
-- For allowing explicit type signatures
{-# LANGUAGE InstanceSigs #-}
-- Disabling warnings since the exercises are not really practical
module Chapter_14 where
import Data.List (intercalate, sort)
import Test.Hspec
import Test.QuickCheck
import Data.Char
-- Validating numbers into words
-- Intermission: Short Exercise
{-
In the Chapter Exercises at the end of Recursion, you were given this exercise: --
--
Write a function that multiplies two numbers using recursive sum- mation. The --
type should be (Eq a, Num a) => a -> a -> a although, depending on how you do --
it, you might also consider adding an Ord constraint. --
--
If you still have your answer, great! If not, rewrite it and then write hspec --
tests for it. --
-}
multBy :: (Num a, Eq a) => a -> a -> a
multBy x 0 = 0
multBy x y = x + multBy x (y - 1)
main1 :: IO ()
main1 = hspec $ do
describe "multBy" $ do
it "5 multiplied by 0 is 0" $ do
multBy 5 0 `shouldBe` 0
it "10 multiplied by 1 is 10" $ do
multBy 10 1 `shouldBe` 10
it "20 multiplied by 10 is 200" $ do
multBy 20 10 `shouldBe` 200
-- Chapter exercises
{-
Remember the “numbers into words” exercise in Recursion? You’ll be writing tests --
to validate the functions you wrote. --
-}
-- Pasting the functions here:
digitToWord :: Int -> String
digitToWord n = digitWords !! n
where digitWords = ["zero", "one", "two", "three", "four"
, "five", "six", "seven", "eight", "nine"]
digits :: Int -> [Int]
digits n = go n []
where go n xs
| n == 0 = xs
| otherwise = go (div n 10) (mod n 10 : xs)
wordNumber :: Int -> String
wordNumber n = intercalate "-" (map digitToWord (digits n))
main2 :: IO ()
main2 = hspec $ do
describe "digitToWord" $ do
it "returns zero for 0" $ do
digitToWord 0 `shouldBe` "zero"
it "returns one for 1" $ do
digitToWord 1 `shouldBe` "one"
describe "digits" $ do
it "returns [1] for 1" $ do
digits 1 `shouldBe` [1]
it "return [1, 0, 0] for 100" $ do
digits 100 `shouldBe` [1, 0, 0]
describe "wordNumber" $ do
it "one-zero-zero given 100" $ do
wordNumber 100 `shouldBe` "one-zero-zero"
it "nine-zero-zero-one for 9001" $ do
wordNumber 9001 `shouldBe` "nine-zero-zero-one"
-- Using QuickCheck
-- 1
-- for a function
half x = x / 2
-- this property should hold
halfIdentity = (*2) . half
-- 2
-- for any list you apply sort to this property should hold
listOrdered :: (Ord a) => [a] -> Bool
listOrdered xs =
snd $ foldr go (Nothing, True) xs
where go _ status@(_, False) = status
go y (Nothing, t) = (Just y, t)
go y (Just x, t) = (Just y, x >= y)
prop_sorting xs = listOrdered xs == (sort xs == xs)
-- 3
-- Now we’ll test the associative and commutative properties of addition:
genericAssociative :: (Eq a) => (a -> a -> a) -> a -> a -> a -> Bool
genericAssociative f x y z = x `f` (y `f` z) == (x `f` y) `f` z
genericCommutative :: (Eq a) => (a -> a -> a) -> a -> a -> Bool
genericCommutative f x y = x `f` y == y `f` x
prop_plusAssociative :: Int -> Int -> Int -> Bool
prop_plusAssociative = genericAssociative (+)
prop_plusCommutative :: Int -> Int -> Bool
prop_plusCommutative = genericCommutative (+)
-- 4
-- Now do the same for multiplication.
prop_multAssociative :: Int -> Int -> Int -> Bool
prop_multAssociative = genericAssociative (*)
prop_multCommutative :: Int -> Int -> Bool
prop_multCommutative = genericCommutative (*)
-- 5
-- We mentioned in one of the first chapters that there are some laws involving
-- the relationship of quot and rem and div and mod. Write QuickCheck tests to
-- prove them
prop_quotRem :: Int -> NonZero Int -> Bool
prop_quotRem x (NonZero y) = quot x y * y + rem x y == x
prop_divMod :: Int -> NonZero Int -> Bool
prop_divMod x (NonZero y) = div x y * y + mod x y == x
-- 6
-- Is (^) associative? Is it commutative? Use QuickCheck to see if the computer
-- can contradict such an assertion.
-- Yes, both of these will fail:
prop_powerAssociative :: Int -> Int -> Int -> Bool
prop_powerAssociative = genericAssociative (^)
prop_powerCommutative :: Int -> Int -> Bool
prop_powerCommutative = genericCommutative (^)
-- 7
-- Test that reversing a list twice is the same as the identity of the list:
prop_doubleReverse :: [Int] -> Bool
prop_doubleReverse xs = (reverse . reverse) xs == xs
-- 8
-- Write a property for the definition of ($).
prop_dollar :: (Eq b) => (a -> b) -> a -> Bool
prop_dollar f a = f a == (f $ a)
prop_dollarDouble :: Int -> Bool
prop_dollarDouble = prop_dollar (* 2)
prop_dot :: (Eq c) => (b -> c) -> (a -> b) -> a -> Bool
prop_dot f g a = (f . g) a == f (g a)
prop_stringToIntToTwice :: String -> Bool
prop_stringToIntToTwice = prop_dot (* 2) length
-- 9
-- See if these two functions are equal:
prop_foldrCons :: [Int] -> [Int] -> Bool
prop_foldrCons xs ys = foldr (:) xs ys == xs ++ ys
-- Fails. Could pass if we flip (++).
prop_foldrConcat :: [[Int]] -> Bool
prop_foldrConcat xs = foldr (++) [] xs == concat xs
-- Passes.
-- 10
-- Hm. Is that so?
prop_lengthTake :: [Int] -> NonNegative Int -> Bool
prop_lengthTake xs (NonNegative n) = length (take n xs) == n
-- Fails.
-- Finally, this is a fun one. You may remember we had you com- pose read and
-- show one time to complete a “round trip.” Well, now you can test that it
-- works:
prop_readShow :: Int -> Bool
prop_readShow x = read (show x) == x
-- Failure
-- Find out why this property fails.
square :: Double -> Double
square x = x * x
-- Works for non negative numbers. Not for negative numbers since sqrt returns
-- positive.
prop_squareSqrt :: NonNegative Double -> Bool
prop_squareSqrt (NonNegative x) = sqrt (square x) == x
-- Doesn't work because sqrt roots are approximations and squaring them doesn't
-- give the same number back.
prop_squareSqrt' :: NonNegative Double -> Bool
prop_squareSqrt' (NonNegative x) = sqrt x * sqrt x == x
-- Idempotence
{-
Idempotence refers to a property of some functions in which the result value --
does not change beyond the initial application. If you apply the function once, --
it returns a result, and applying the same function to that value won’t ever --
change it. You might think of a list that you sort: once you sort it, the sorted --
list will remain the same after applying the same sorting function to it. It’s --
already sorted, so new applications of the sort function won’t change it. --
--
Use QuickCheck and the following helper functions to demonstrate idempotence for --
the following: --
-}
twice f = f . f
fourTimes = twice . twice
-- 1
capitalizeWord :: String -> String
capitalizeWord [] = []
capitalizeWord (x:xs) = toUpper x : xs
f :: String -> Bool
f x =
(capitalizeWord x == twice capitalizeWord x)
&&
(capitalizeWord x == fourTimes capitalizeWord x)
f' :: [Int] -> Bool
f' x =
(sort x == twice sort x)
&&
(sort x == fourTimes sort x)
-- Make a Gen random generator for the datatype
{-
We demonstrated in the chapter how to make Gen generators for different
datatypes. We are so certain you enjoyed that, we are going to ask you to do it
for some new datatypes:
-}
-- 1
-- Equal probabilities for each.
data Fool =
Fulse
| Frue
deriving (Eq, Show)
genFool :: Gen Fool
genFool = elements [Fulse, Frue]
-- 2
-- 2/3s chance of Fulse, 1/3 chance of Frue.
genFool' :: Gen Fool
genFool' = frequency [ (2, return Fulse)
, (1, return Frue) ]
-- Hangman testing
{-
Next, you should go back to the hangman project from the previous chapter and
write tests. The kinds of tests you can write at this point will be limited due
to the interactive nature of the game. However, you can test the
functions. Focus your attention on testing the following: fillInCharacter and
handleGuess.
-}
-- See the tests file in the hangman project.
-- Validating ciphers
{-
As a final exercise, create QuickCheck properties that verify your Caesar and
Vigenère ciphers return the same data after encoding and decoding a string.
-}
cipherChar :: Int -> Char -> Char
cipherChar n c =
let baseIndex = if isUpper c then ord 'A' else ord 'a'
index = ord c - baseIndex
newIndex = mod (index + n) 26
in chr $ newIndex + baseIndex
cipher n = map $ cipherChar n
unCipherChar n = cipherChar (26 - mod n 26)
unCipher n = map $ unCipherChar n
vigenere :: String -> String -> String
vigenere key str = go str 0
where
go [] _ = []
go (' ':xs) index = ' ' : go xs index
go (x:xs) index =
let
char = (key !! index)
baseIndex = if isUpper char then ord 'A' else ord 'a'
offset = ord char - baseIndex
in
cipherChar offset x : go xs (mod (index + 1) (length key))
unVigenere :: String -> String -> String
unVigenere key str = go str 0
where
go [] _ = []
go (' ':xs) index = ' ' : go xs index
go (x:xs) index =
let
char = (key !! index)
baseIndex = if isUpper char then ord 'A' else ord 'a'
offset = ord char - baseIndex
in
unCipherChar offset x : go xs (mod (index + 1) (length key))
newtype AlphabetString = AlphabetString { unAlphabetString :: String }
deriving (Eq, Show)
instance Arbitrary AlphabetString where
arbitrary :: Gen AlphabetString
arbitrary = fmap AlphabetString (listOf $ elements ['a' .. 'z'])
newtype NonEmptyAlphabetString =
NonEmptyAlphabetString { unNonEmptyAlphabetString :: String }
deriving (Eq, Show)
instance Arbitrary NonEmptyAlphabetString where
arbitrary :: Gen NonEmptyAlphabetString
arbitrary = fmap NonEmptyAlphabetString (listOf1 $ elements ['a' .. 'z'])
prop_caesarIdentity :: Int -> AlphabetString -> Bool
prop_caesarIdentity n (AlphabetString str) = str == (unCipher n . cipher n) str
prop_vigenereIdentity :: NonEmptyAlphabetString -> AlphabetString -> Bool
prop_vigenereIdentity (NonEmptyAlphabetString key) (AlphabetString str) =
str == (unVigenere key . vigenere key) str