Module

BaseRationals

Package
purescript-base-rationals
Repository
herrzinter/purescript-base-rationals

Implements in arbitrary basis given arbitrary digits:

• parsing a string for a rational
• rendering a non-fractional string represenation of a rational

Digits can be created from an `Array` of `Char`s.

``````let digits  = digitsFromArray ['0', '1', '2', 'A', 'B']
``````

`toString` and `fromString` run both in the `(Either String)` monad, providing `String` error messages. Both have `Digits` and a basis as `Int` as their first two arguments; and a `String` or a `PreciseRational` as third one respectively. A usage example is:

``````string :: Either String String
string = do
let pr = PR.fromInts 1 7 :: PreciseRational
let basis = 4
s <-
pure s

pr :: Either String PreciseRational
pr = do
let s = "A2AB01.20B1A" :: String
let basis = 5
pr <- fromString digits basis s
``````

#DigitsSource

``data Digits``

Container type for an `Array` of `Chars` representing digits. The constructor is hidden, as digits are more constrained than an `Array` of `Char`s, use `digitsFromArray` instead.

Instances

• `Show Digits`

#digitsFromArraySource

``digitsFromArray :: Array Char -> Either String Digits``

Wrap `Array` of `Char`s in digit container, if array

• contains at least two digits, as the minimum basis is two
• all contained digits need to be unique

#arrayFromDigitsSource

``arrayFromDigits :: Digits -> Array Char``

Unwrap `Array` of `Char`s from `Digits` container

#maximalBasisOfDigitsSource

``maximalBasisOfDigits :: Digits -> Int``

Get the maximal possible basis for `Digits`. It equals the length of the wrapped `Array` of `Char`s

#fromStringSource

``fromString :: Digits -> Int -> String -> Either String PreciseRational``

Parse a `PreciseRational` from a `String` in basis `Int` given `Digits`.

#toStringSource

``toString :: Digits -> Int -> PreciseRational -> Either String String``

Render a non-fractional `String`-representation of a `PreciseRational` in basis `Int` given `Digits`.

#indexSource

``index :: Digits -> BigInt -> Either String Char``

Lookup the digit `Char` with index `BigInt` in `Digits`

#digitIndexSource

``digitIndex :: Char -> Digits -> Either String BigInt``

Lookup the index `BigInt` of the first occurence of `Char` in `Digits`