@@ -18,7 +18,7 @@ import Data.List
18
18
-- +++ OK, passed 1 test.
19
19
20
20
isSorted :: (Show a , Ord a ) => [a ] -> Property
21
- isSorted = todo
21
+ isSorted xs = sort xs === xs
22
22
23
23
------------------------------------------------------------------------------
24
24
-- Ex 2: In this and the following exercises, we'll build a suite of
@@ -50,7 +50,7 @@ isSorted = todo
50
50
-- +++ OK, passed 1 test.
51
51
52
52
sumIsLength :: Show a => [a ] -> [(a ,Int )] -> Property
53
- sumIsLength input output = todo
53
+ sumIsLength input output = length input === sum ( map snd output)
54
54
55
55
-- This is a function that passes the sumIsLength test but is wrong
56
56
freq1 :: Eq a => [a ] -> [(a ,Int )]
@@ -79,7 +79,7 @@ freq1 (x:y:xs) = [(x,1),(y,length xs + 1)]
79
79
-- +++ OK, passed 100 tests.
80
80
81
81
inputInOutput :: (Show a , Eq a ) => [a ] -> [(a ,Int )] -> Property
82
- inputInOutput input output = todo
82
+ inputInOutput input output = forAll (elements input) ( \ x -> x `elem` map fst output)
83
83
84
84
-- This function passes both the sumIsLength and inputInOutput tests
85
85
freq2 :: Eq a => [a ] -> [(a ,Int )]
@@ -110,7 +110,7 @@ freq2 xs = map (\x -> (x,1)) xs
110
110
-- +++ OK, passed 100 tests.
111
111
112
112
outputInInput :: (Show a , Eq a ) => [a ] -> [(a ,Int )] -> Property
113
- outputInInput input output = todo
113
+ outputInInput input output = forAll (elements output) ( \ (x,n) -> n === length ( filter ( == x) input))
114
114
115
115
-- This function passes the outputInInput test but not the others
116
116
freq3 :: Eq a => [a ] -> [(a ,Int )]
@@ -139,7 +139,8 @@ freq3 (x:xs) = [(x,1 + length (filter (==x) xs))]
139
139
-- +++ OK, passed 100 tests.
140
140
141
141
frequenciesProp :: ([Char ] -> [(Char ,Int )]) -> NonEmptyList Char -> Property
142
- frequenciesProp freq input = todo
142
+ frequenciesProp freq input = conjoin $ map ($ freq xs) [sumIsLength xs, inputInOutput xs, outputInInput xs]
143
+ where xs = getNonEmpty input
143
144
144
145
frequencies :: Eq a => [a ] -> [(a ,Int )]
145
146
frequencies [] = []
@@ -170,7 +171,10 @@ frequencies (x:ys) = (x, length xs) : frequencies others
170
171
-- [2,4,10]
171
172
172
173
genList :: Gen [Int ]
173
- genList = todo
174
+ genList = do
175
+ n <- choose (3 ,5 )
176
+ xs <- vectorOf n (choose (0 ,10 ))
177
+ return $ sort xs
174
178
175
179
------------------------------------------------------------------------------
176
180
-- Ex 7: Here are the datatypes Arg and Expression from Set 15. Write
@@ -208,7 +212,7 @@ data Expression = Plus Arg Arg | Minus Arg Arg
208
212
deriving (Show , Eq )
209
213
210
214
instance Arbitrary Arg where
211
- arbitrary = todo
215
+ arbitrary = oneof [ Number <$> choose ( 0 , 10 ), Variable <$> elements " abcxyz " ]
212
216
213
217
instance Arbitrary Expression where
214
- arbitrary = todo
218
+ arbitrary = oneof [ Plus <$> arbitrary <*> arbitrary, Minus <$> arbitrary <*> arbitrary]
0 commit comments