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 Chars.

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

#Digits Source

data Digits

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

Instances

#digitsFromArray Source

digitsFromArray :: Array Char -> Either String Digits

Wrap Array of Chars in digit container, if array

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

#arrayFromDigits Source

arrayFromDigits :: Digits -> Array Char

Unwrap Array of Chars from Digits container

#maximalBasisOfDigits Source

maximalBasisOfDigits :: Digits -> Int

Get the maximal possible basis for Digits. It equals the length of the wrapped Array of Chars

#fromString Source

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

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

#toString Source

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

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

#index Source

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

Lookup the digit Char with index BigInt in Digits

#digitIndex Source

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

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