实验要求
分数的运算
module MyFraction where
import Test.QuickCheck
import Prelude hiding ((<+>))
import Prelude hiding ((<->))
import Prelude hiding ((<*>))
import Prelude hiding ((</>))
import Prelude hiding ((<==>))
type Fraction = (Integer, Integer)
ratplus :: Fraction -> Fraction -> Fraction
ratminus :: Fraction -> Fraction -> Fraction
rattimes :: Fraction -> Fraction -> Fraction
ratdiv :: Fraction -> Fraction -> Fraction
ratfloor :: Fraction -> Integer
ratfloat :: Fraction -> Float
rateq :: Fraction -> Fraction -> Bool
ratgcd :: Fraction -> Fraction
first :: Fraction -> Integer
second :: Fraction -> Integer
(<+>) :: Fraction -> Fraction -> Fraction
(<->) :: Fraction -> Fraction -> Fraction
(<*>) :: Fraction -> Fraction -> Fraction
(</>) :: Fraction -> Fraction -> Fraction
(<==>) :: Fraction -> Fraction -> Bool
infixl 7 <*>,</>
infixl 6 <+>,<->
infixl 5 <==>
ratgcd (x1,x2) = (div x1 (gcd x1 x2),div x2 (gcd x1 x2))
ratplus (a1,a2)(a3,a4) = ratgcd(a1*a4+a3*a2,a2*a4)
ratminus (b1,b2)(b3,b4) = ratgcd(b1*b4-b3*b2,b2*b4)
rattimes (c1,c2)(c3,c4) = ratgcd(c1*c3,c2*c4)
ratdiv (d1,d2)(d3,d4) = ratgcd(d1*d4,d2*d3)
ratfloor (e1,e2) = div e1 e2
ratfloat (f1,f2) = (fromInteger f1) / (fromInteger f2)
first (num1,num2) = num1
second (num3,num4) = num4
rateq (g1,g2)(g3,g4) = if (h1 == h3) && (h2 == h4) then True else False
where h1 = first(ratgcd(g1,g2))
h2 = second(ratgcd(g1,g2))
h3 = first(ratgcd(g3,g4))
h4 = second(ratgcd(g3,g4))
j1 <+> j2 = ratplus j1 j2
k1 <-> k2 = ratminus k1 k2
l1 <*> l2 = rattimes l1 l2
m1 </> m2 = ratdiv m1 m2
n1 <==> n2 = rateq n1 n2
prop_pluschange :: Fraction -> Fraction -> Bool
prop_timeschange :: Fraction -> Fraction -> Bool
prop_pluschange (test1,test2)(test3,test4) | (test2 == 0) || (test4 == 0) = True
|otherwise = ratplus (test1,test2)(test3,test4) == ratplus (test3,test4)(test1,test2)
prop_timeschange (test1,test2)(test3,test4) | (test2 == 0) || (test4 == 0) = True
|otherwise = rattimes (test1,test2)(test3,test4) == rattimes (test3,test4)(test1,test2)
Newton-Raphson
module NewtonRaphson where
squareroot2 :: Float -> Integer -> Float
squareroot :: Float -> Float -> Integer -> Float
sqrtSeq :: Float -> Float -> [Float]
squareroot' :: Float -> Float -> Float -> Float
iter :: Float -> Float -> [Float]
iter x0 r = iterate (\x -> ((x + r / x) / 2)) x0 --迭代序列
squareroot2 x1 n1 | (x1 > 0) && (n1 > 0) = last $ take (fromEnum n1) (iterate (\x -> (x + 2/x) / 2) (x1))
| otherwise = error ("The argument must be positive")
squareroot r1 x2 n2 | (x2 > 0) && (n2 > 0) && (r1 > 0) = last $ take (fromEnum n2) $ iter x2 r1
| otherwise = error ("The argument must be positive")
sqrtSeq r2 x3 | (x3 > 0) && (r2 > 0) = iter x3 r2
| otherwise = error ("The argument must be positive")
squareroot' r3 x4 eps | eps >= r3 = error ("The epsilon must be less than R")
| (x4 > 0) && (r3 > 0) = root
| otherwise = error ("The argument must be positive")
where
root = head rootlist --取出第一个符合条件的数
rootlist = [ xa | (xa,xb) <- comparelist , abs(xa - xb) < eps] --列出所有相邻两项之差小于epsilon的数
xnlist = iter x4 r3 --xn数组
xn_1list = 0:(init xnlist) --(xn-1)数组
comparelist = zip xnlist xn_1list --组合成(xn,xn-1)的数组
未经允许不得转载:MikuAlpha's Blog » 分数的运算&Newton-Raphson-Haskell实验一