packages feed

kempe-0.1.1.2: lib/rational.kmp

import"prelude/fn.kmp"
import"lib/numbertheory.kmp"

; Slightly suspect rational based on 'Int' type
type Rational { Rational Int Int }

unRational : Rational -- Int Int
           =: [ { case | Rational -> } ]

multRat : Rational Rational -- Rational
        =: [ dip(unRational) unRational dip(swap *) * Rational reduce ]

reduce : Rational -- Rational
       =: [ unRational dup2 gcd dup dip(swap dip(/)) / Rational ]

addRat : Rational Rational -- Rational
       =: [ dip(unRational) unRational dip(swap)
            dup2 * dip(swap dip(swap) * dip(*) +)
            Rational reduce
          ]

%foreign kabi multRat
%foreign kabi addRat
%foreign kabi reduce