1
1
module Addition where
2
2
3
- -- import Test.Hspec
4
- import Test.QuickCheck
3
+ import Test.Hspec
5
4
5
+ -- Example 1
6
+ -- main :: IO ()
7
+ -- main = hspec $ do
8
+ -- describe "Addition" $ do
9
+ -- it "1 + 1 is greater than 1" $ do
10
+ -- (1 + 1) > 1 `shouldBe` True
11
+ -- it "2 + 2 is equal to 4" $ do
12
+ -- 2 + 2 `shouldBe` 4
13
+
14
+ -- Example 2
6
15
-- dividedBy :: Integral a => a -> a -> (a, a)
7
16
-- dividedBy num denom = go num denom 0
8
- -- where go n d count
9
- -- | n < d = (count, n)
10
- -- | otherwise = go (n - d) d (count + 1)
17
+ -- where
18
+ -- go n d count
19
+ -- | n < d = (count, n)
20
+ -- | otherwise = go (n - d) d (count + 1)
11
21
--
12
22
-- main :: IO ()
13
23
-- main = hspec $ do
@@ -17,33 +27,32 @@ import Test.QuickCheck
17
27
-- it "22 divided by 5 is 4 remainder 2" $ do
18
28
-- dividedBy 22 5 `shouldBe` (4, 2)
19
29
20
- -- main :: IO ()
21
- -- main = hspec $ do
22
- -- describe "Addition" $ do
23
- -- it "1 + 1 is greater than 1" $ do
24
- -- (1 + 1) > 1 `shouldBe` True
25
- -- it "2 + 2 is equal to 4" $ do
26
- -- 2 + 2 `shouldBe` 4
27
- -- it "x + 1 is always greater than x" $ do
28
- -- property $ \x -> x + 1 > (x :: Int)
29
-
30
- -- choose :: System.Random.Random a => (a, a) -> Gen a
31
- -- elements :: [a] -> Gen a
32
-
33
- -- genBool :: Gen Bool
34
- -- genBool = choose (False, True)
35
- --
36
- -- genBool' :: Gen Bool
37
- -- genBool' = elements [False, True]
38
- --
39
- -- genOrdering :: Gen Ordering
40
- -- genOrdering = elements [LT, EQ, GT]
41
- --
42
- -- genChar :: Gen Char
43
- -- genChar = elements ['a'..'z']
44
-
45
- prop_additionGreater :: Int -> Bool
46
- prop_additionGreater x = x + 1 > x
30
+ -- Short Exercise
31
+ myMult :: (Eq a , Num a ) => a -> a -> a
32
+ myMult x y
33
+ | sy == 0 || sy == 1 = go x y 0
34
+ | sy == (- 1 ) && (sx == 0 || sx == 1 ) = go y x 0
35
+ | otherwise = go ax ay 0
36
+ where
37
+ sx = signum x
38
+ sy = signum y
39
+ ax = abs x
40
+ ay = abs y
41
+ go _ 0 ab = ab
42
+ go a b ab = go a (b - 1 ) (a + ab)
47
43
48
- runQc :: IO ()
49
- runQc = quickCheck prop_additionGreater
44
+ main :: IO ()
45
+ main = hspec $ do
46
+ describe " Multiplication" $ do
47
+ it " 5 multiplied by 0 is 0" $ do
48
+ myMult 5 0 `shouldBe` 0
49
+ it " 0 multiplied by 5 is 0" $ do
50
+ myMult 0 5 `shouldBe` 0
51
+ it " 2 multiplied by 3 is 6" $ do
52
+ myMult 2 3 `shouldBe` 6
53
+ it " -2 multiplied by 3 is -6" $ do
54
+ myMult (- 2 ) 3 `shouldBe` (- 6 )
55
+ it " 2 multiplied by -3 is -6" $ do
56
+ myMult 2 (- 3 ) `shouldBe` (- 6 )
57
+ it " -2 multiplied by -3 is 6" $ do
58
+ myMult (- 2 ) (- 3 ) `shouldBe` 6
0 commit comments