CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
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
--import Test.QuickCheck
module MyFraction where
Fraction=(Integer,Integer)
--分式化简
ratreduction::Fraction->Fraction
ratreduction(x,y)=(div x g,div y g)where g=gcd x y
--加法
ratplus::Fraction->Fraction->Fraction
ratplus(a,b)(c,d)=ratreduction(a*d+b*c,b*d)
--减法,调用加法实现
ratminus::Fraction->Fraction->Fraction
ratminus(a,b)(c,d)=ratplus(a,b)(-c,d)
--乘法
rattimes::Fraction->Fraction->Fraction
rattimes(a,b)(c,d)=ratreduction(a*c,b*d)
--除法,调用乘法实现
ratdiv::Fraction->Fraction->Fraction
ratdiv(a,b)(c,d)=rattimes(a,b)(d,c)
--取整
ratfloor::Fraction->Integer
ratfloor(a,b)=div a b
--转成浮点
ratfloat::Fraction->Float
ratfloat(a,b)=fromInteger a/fromInteger b
--判断相等
rateq::Fraction->Fraction->Bool
rateq(a,b)(c,d)=a*d==b*c
--重载运算符
(<+>)::Fraction->Fraction->Fraction
(a,b)<+>(c,d)=ratplus(a,b)(c,d)
(<->)::Fraction->Fraction->Fraction
(a,b)<->(c,d)=ratminus(a,b)(c,d)
(<-*->)::Fraction->Fraction->Fraction
(a,b)<-*->(c,d)=rattimes(a,b)(c,d)
(</>)::Fraction->Fraction->Fraction
(a,b)</>(c,d)=ratdiv(a,b)(c,d)
(<==>)::Fraction->Fraction->Bool
(a,b)<==>(c,d)=rateq(a,b)(c,d)
--确定运算优先级
infixl 6 <-*->,</>
infixl 5 <+>,<->
infixl 4 <==>
{-
--检查任意分数+(0,1)之后仍然等于其本身
prop_ratplus_unit :: Fraction -> Property
prop_ratplus_unit (a,b) = b > 0 ==>(a, b) <+> (0,1) <==> (a, b)
--检查任意分数加上相反数之后为0
prop_ratplus_inv :: Fraction -> Property
prop_ratplus_inv (a,b) = b > 0 ==>(a, b) <+> (-a,b) <==> (0, 1)
--检查任意非零分数乘上倒数之后为1
prop_rattimes_inv :: Fraction -> Property
prop_rattimes_inv (a,b) = a/=0&&b > 0 ==>(a, b) <-*-> (b,a) <==> (1, 1)
-- 检查乘法分配律
prop_rattimes_plus_distr :: Fraction -> Fraction -> Fraction ->Property
prop_rattimes_plus_distr (a,b) (c,d) (e,f) = b > 0 && d > 0 && f > 0 ==> (a,b) <-*-> ((c,d) <+> (e,f)) <==> ((a,b) <-*-> (c,d)) <+> ((a,b) <-*-> (e,f))
-}