{-  Exploring Languages with Interpreters and Functional Programming
    Chapter 7 & 12: Rational Arithmetic Module (RationalCore)
    Copyright (C) 2018, H. Conrad Cunningham

1234567890123456789012345678901234567890123456789012345678901234567890

2016-07-??: Based on SICP 2.1 & my earler Haskell, Lua versions
2017-02-03: Added comments
2018-06-12: Added "zeroRat" and divide by zero check
2018-07-04: Renamed from Rational to Rational1/Rational2;
            Added copyright notice
2018-07-19: Updated for compatibility with Chapter 12

-}

module Rational1
  ( Rat, zeroRat, makeRat, numer, denom, showRat,
    negRat, addRat, subRat, mulRat, divRat, eqRat )
where

-- Select needed data representation module
import RationalCore         -- for Rational1
-- import RationalDeferGCD  -- for Rational2

-- First-order Operations

negRat :: Rat -> Rat 
negRat x = makeRat (- numer x) (denom x)

addRat, subRat, mulRat, divRat :: Rat -> Rat -> Rat 
addRat x y = makeRat (numer x * denom y + numer y * denom x)
                     (denom x * denom y) 
subRat x y = makeRat (numer x * denom y - numer y * denom x)
                     (denom x * denom y) 
mulRat x y = makeRat (numer x * numer y) (denom x * denom y) 
divRat x y
    | eqRat y zeroRat = error "Attempt to divide by 0"
    | otherwise       = makeRat (numer x * denom y)
                                (denom x * numer y) 
eqRat :: Rat -> Rat -> Bool 
eqRat x y = (numer x) * (denom y) == (numer y) * (denom x)      
