Skip to content

Commit

Permalink
Incorporated review comments from #1193
Browse files Browse the repository at this point in the history
  • Loading branch information
tofische committed Jan 19, 2024
1 parent d52ec7c commit 9ec326e
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 229 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module RationalNumbers
(Rational,
abs,
reduce,
numerator,
denominator,
add,
sub,
mul,
div,
exprational,
expreal,
pow,
expRational,
expReal,
rational) where

import Prelude hiding (div, abs, Rational)
Expand All @@ -16,35 +18,40 @@ import qualified Prelude as P
-- Data definition -------------------------------------------------------------
data Rational a = Rational a a deriving(Eq, Show)

rational :: (a, a) -> Rational a
rational (n, d) = Rational n d
rational :: Integral a => (a, a) -> Rational a
rational (n, d) = Rational (n' `quot` g) (d' `quot` g)
where
g = gcd n d
(n', d') = if d < 0 then (-n, -d) else (n, d)

-- unary operators -------------------------------------------------------------
abs :: Integral a => Rational a -> Rational a
abs (Rational n d ) = reduce $ Rational (P.abs n) (P.abs d)
abs (Rational n d) = rational (P.abs n, P.abs d)

reduce :: Integral a => Rational a -> Rational a
reduce (Rational n d) = Rational (n' `quot` g) (d' `quot` g)
where
g = gcd n d
(n', d') = if d < 0 then (-n, -d) else (n, d)
numerator :: Integral a => Rational a -> a
numerator (Rational n _) = n

denominator :: Integral a => Rational a -> a
denominator (Rational _ d) = d

-- binary operators ------------------------------------------------------------
add :: Integral a => Rational a -> Rational a -> Rational a
add (Rational n1 d1) (Rational n2 d2) = let dd = d1*d2 in reduce $ Rational (n1*d2 + n2*d1) dd
add (Rational n1 d1) (Rational n2 d2) = let dd = d1*d2 in rational (n1*d2 + n2*d1, dd)

sub :: Integral a => Rational a -> Rational a -> Rational a
sub (Rational n1 d1) (Rational n2 d2) = let dd = d1*d2 in reduce $ Rational (n1*d2 - n2*d1) dd
sub (Rational n1 d1) (Rational n2 d2) = let dd = d1*d2 in rational (n1*d2 - n2*d1, dd)

mul :: Integral a => Rational a -> Rational a -> Rational a
mul (Rational n1 d1) (Rational n2 d2) = reduce $ Rational (n1*n2) (d1*d2)
mul (Rational n1 d1) (Rational n2 d2) = rational (n1*n2, d1*d2)

div :: Integral a => Rational a -> Rational a -> Rational a
div (Rational n1 d1) (Rational n2 d2) = reduce $ Rational (n1*d2) (d1*n2)
div (Rational n1 d1) (Rational n2 d2) = rational (n1*d2, d1*n2)

pow :: Integral a => Rational a -> a -> Rational a
pow (Rational n d) num = if num >= 0 then rational (n^num, d^num) else rational (d^(-num), n^(-num))

exprational :: Integral a => Rational a -> a -> Rational a
exprational (Rational n d) num = reduce $ if num >= 0 then Rational (n^num) (d^num) else Rational (d^(-num)) (n^(-num))
expRational :: Integral a => Floating b => Rational a -> b -> b
expRational (Rational n d) num = (fromIntegral n / fromIntegral d) ** num

expreal :: Floating a => Integral b => a -> Rational b -> a
expreal num (Rational n d) = num ** (fromIntegral n / fromIntegral d)
expReal :: Floating a => Integral b => a -> Rational b -> a
expReal num (Rational n d) = num ** (fromIntegral n / fromIntegral d)
27 changes: 17 additions & 10 deletions exercises/practice/rational-numbers/src/RationalNumbers.hs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
module RationalNumbers
(Rational,
abs,
reduce,
numerator,
denominator,
add,
sub,
mul,
div,
exprational,
expreal,
pow,
expRational,
expReal,
rational) where

import Prelude hiding (div, abs, Rational)

-- Data definition -------------------------------------------------------------
data Rational a = Dummy deriving(Eq, Show)

rational :: (a, a) -> Rational a
rational :: Integral a => (a, a) -> Rational a
rational = error "You need to implement this function"

-- unary operators -------------------------------------------------------------
abs :: Integral a => Rational a -> Rational a
abs = error "You need to implement this function"

reduce :: Integral a => Rational a -> Rational a
reduce = error "You need to implement this function"
numerator :: Integral a => Rational a -> a
numerator = error "You need to implement this function"

denominator :: Integral a => Rational a -> a
denominator = error "You need to implement this function"

-- binary operators ------------------------------------------------------------
add :: Integral a => Rational a -> Rational a -> Rational a
Expand All @@ -38,9 +43,11 @@ mul = error "You need to implement this function"
div :: Integral a => Rational a -> Rational a -> Rational a
div = error "You need to implement this function"

exprational :: Integral a => Rational a -> a -> Rational a
exprational = error "You need to implement this function"
pow :: Integral a => Rational a -> a -> Rational a
pow = error "You need to implement this function"

expreal :: Floating a => Integral b => a -> Rational b -> a
expreal = error "You need to implement this function"
expRational :: Integral a => Floating b => Rational a -> b -> b
expRational = error "You need to implement this function"

expReal :: Floating a => Integral b => a -> Rational b -> a
expReal = error "You need to implement this function"
Loading

0 comments on commit 9ec326e

Please sign in to comment.