Skip to content

Commit 1261f09

Browse files
committed
Redo 14.1 to 14.3
1 parent 9901c55 commit 1261f09

File tree

4 files changed

+56
-48
lines changed

4 files changed

+56
-48
lines changed

ch14/addition/Addition.hs

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
module Addition where
22

3-
-- import Test.Hspec
4-
import Test.QuickCheck
3+
import Test.Hspec
54

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
615
-- dividedBy :: Integral a => a -> a -> (a, a)
716
-- 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)
1121
--
1222
-- main :: IO ()
1323
-- main = hspec $ do
@@ -17,33 +27,32 @@ import Test.QuickCheck
1727
-- it "22 divided by 5 is 4 remainder 2" $ do
1828
-- dividedBy 22 5 `shouldBe` (4, 2)
1929

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)
4743

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

ch14/addition/LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright Author name here (c) 2016
1+
Copyright Dwayne Crooks (c) 2016
22

33
All rights reserved.
44

@@ -13,7 +13,7 @@ modification, are permitted provided that the following conditions are met:
1313
disclaimer in the documentation and/or other materials provided
1414
with the distribution.
1515

16-
* Neither the name of Author name here nor the names of other
16+
* Neither the name of Dwayne Crooks nor the names of other
1717
contributors may be used to endorse or promote products derived
1818
from this software without specific prior written permission.
1919

ch14/addition/addition.cabal

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ name: addition
22
version: 0.1.0.0
33
synopsis: Simple project template from stack
44
description: Please see README.md
5-
homepage: https://github.com/githubuser/addition#readme
5+
homepage: https://github.com/dwayne/addition#readme
66
license: BSD3
77
license-file: LICENSE
8-
author: Author name here
9-
maintainer: example@example.com
10-
copyright: 2016 Author name here
8+
author: Dwayne Crooks
9+
maintainer: me@dwaynecrooks.com
10+
copyright: 2016 Dwayne Crooks
1111
category: Text
1212
build-type: Simple
1313
cabal-version: >=1.10
1414

1515
library
16-
hs-source-dirs: .
17-
exposed-modules: Addition
18-
ghc-options: -Wall -fwarn-tabs
19-
default-language: Haskell2010
20-
build-depends: base >=4.7 && <5
21-
, hspec
22-
, QuickCheck
16+
hs-source-dirs: .
17+
exposed-modules: Addition
18+
ghc-options: -Wall -fwarn-tabs
19+
default-language: Haskell2010
20+
build-depends: base >= 4.7 && < 5
21+
, hspec

ch14/addition/stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# resolver:
1616
# name: custom-snapshot
1717
# location: "./custom-snapshot.yaml"
18-
resolver: lts-5.15
18+
resolver: lts-5.17
1919

2020
# User packages to be built.
2121
# Various formats can be used as shown in the example below.

0 commit comments

Comments
 (0)