-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path2.hs
88 lines (74 loc) · 2.49 KB
/
2.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
-- % cabal install hspec
-- % runghc <this_file>
import Small
main :: IO ()
main = hspec $ do
describe "my_soap_iter" $
prop "behaves as model" $ \(Small n) -> n >= 0
==> my_soap_iter n == sum [0..n]
describe "my_fact_iter" $
prop "behaves as model" $ \(Small n) -> n >= 1
==> my_fact_iter n == product [1..n]
describe "my_mul_iter" $
prop "behaves as model" $ \(Small m) (Small n) -> m >= 1 && n >= 1
==> my_mul_iter m n == m * n
describe "my_plus_iter" $
prop "behaves as model" $ \(Small m) (Small n) -> m >= 0 && n >= 0
==> my_plus_iter m n == m + n
describe "my_minus_iter" $
prop "behaves as model" $ \(Small m) (Small n) -> m >= 0 && n >= 0
==> my_minus_iter m n == m - n
describe "my_power_iter" $
prop "behaves as model" $ \(Small m) (Small n) -> m >= 1 && n >= 0
==> my_power_iter m n == m ^ n
-- my_soap :: Integer -> Integer
-- my_soap 0 = 0
-- my_soap n = my_soap (n-1) + n
my_soap_iter :: Integer -> Integer
my_soap_iter x = iter x 0
where
iter :: Integer -> Integer -> Integer
iter 0 acc = acc
iter n acc = iter (n-1) (acc + n)
-- my_fact :: Integer -> Integer
-- my_fact 1 = 1
-- my_fact n = my_fact (n - 1) * n
my_fact_iter :: Integer -> Integer
my_fact_iter x = iter x 1
where
iter :: Integer -> Integer -> Integer
iter 1 acc = acc
iter n acc = iter (n - 1) (acc * n)
-- my_mul :: Integer -> Integer -> Integer
-- my_mul m 1 = m
-- my_mul m n = my_mul m (n - 1) + m
my_mul_iter :: Integer -> Integer -> Integer
my_mul_iter x y = iter x y x
where
iter :: Integer -> Integer -> Integer -> Integer
iter _ 1 acc = acc
iter m n acc = iter m (n - 1) (acc + m)
-- my_plus :: Integer -> Integer -> Integer
-- my_plus m 0 = m
-- my_plus m n = my_plus m (n - 1) + 1
my_plus_iter :: Integer -> Integer -> Integer
my_plus_iter x y = iter x y undefined
where
iter :: Integer -> Integer -> Integer -> Integer
iter = undefined
-- my_minus :: Integer -> Integer -> Integer
-- my_minus m 0 = m
-- my_minus m n = my_minus m (n - 1) - 1
my_minus_iter :: Integer -> Integer -> Integer
my_minus_iter x y = iter x y undefined
where
iter :: Integer -> Integer -> Integer -> Integer
iter = undefined
-- my_power :: Integer -> Integer -> Integer
-- my_power _ 0 = 1
-- my_power m n = my_power m (n - 1) * m
my_power_iter :: Integer -> Integer -> Integer
my_power_iter x y = iter x y undefined
where
iter :: Integer -> Integer -> Integer -> Integer
iter = undefined