File tree Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Original file line number Diff line number Diff line change 1
- module Data.Ratio where
1
+ module Data.Ratio
2
+ ( Ratio (Ratio)
3
+ , numerator
4
+ , denominator
5
+ , gcd
6
+ ) where
2
7
3
8
import Prelude
4
9
@@ -27,3 +32,9 @@ numerator (Ratio a _) = a
27
32
28
33
denominator :: forall a . Ratio a -> a
29
34
denominator (Ratio _ b) = b
35
+
36
+ gcd :: forall a . (Eq a , EuclideanRing a ) => Ratio a -> a
37
+ gcd (Ratio m n)
38
+ | n == zero = m
39
+ | otherwise = gcd (Ratio n (m `mod` n))
40
+
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ module Data.Rational
8
8
9
9
import Prelude
10
10
import Data.Int as Int
11
- import Data.Ratio (Ratio (Ratio))
11
+ import Data.Ratio (Ratio (Ratio), gcd )
12
12
13
13
newtype Rational = Rational (Ratio Int )
14
14
@@ -55,16 +55,11 @@ fromInt :: Int -> Rational
55
55
fromInt i = Rational $ Ratio i 1
56
56
57
57
reduce :: Rational -> Rational
58
- reduce (Rational (Ratio a b)) =
59
- let x = a / gcd a b
60
- y = b / gcd a b
58
+ reduce (Rational ratio@ (Ratio a b)) =
59
+ let x = a / gcd ratio
60
+ y = b / gcd ratio
61
61
in Rational $ Ratio (x * signum y) (abs y)
62
62
63
- gcd :: Int -> Int -> Int
64
- gcd m n
65
- | n == 0 = m
66
- | otherwise = gcd n (m `mod` n)
67
-
68
63
signum :: Int -> Int
69
64
signum 0 = 0
70
65
signum x' | x' < 0 = -1
You can’t perform that action at this time.
0 commit comments