Possible bug/omission in Numeric library?

Karl-Filip Faxen kff@it.kth.se
Wed, 28 Nov 2001 16:48:55 +0100


In fact, we might want to be slightly more general. Why don't go for
a general base conversion, much like Malcolms idea but without converting
every digit to a Char. Say something like

   toBase :: (Integral a) => a -> a -> [a]
   toBase base n
      | n < 0     = error "Numeric.toBase: can't convert negative numbers"
      | otherwise = theDigits n []
      where theDigits n r = let (n',d) = quotRem n base
                                r'     = d : r
                            in if n' == 0 then r' else theDigits n' r'

Then the showInt function becomes

    showInt :: (Integral a) => a -> (Int->Char) -> a -> ShowS
    showInt base intToDig n r = foldr (\ i r -> intToDig (fromIntegral i) : r)
                                      (toBase base n)

but we can also use toBase by itself. We could also have an analogous 
fromBase, of course.