-showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
-showIntAtBase base toChr n r
- | n < 0 = error ("Numeric.showIntAtBase: applied to negative number " ++ show n)
- | otherwise =
- case quotRem n base of { (n', d) ->
- let c = toChr d in
- seq c $ -- stricter than necessary
- let
- r' = c : r
- in
- if n' == 0 then r' else showIntAtBase base toChr n' r'
- }
-
-showHex, showOct :: Integral a => a -> ShowS
-showHex = showIntAtBase 16 (intToDigit.fromIntegral)
-showOct = showIntAtBase 8 (intToDigit.fromIntegral)
+-- | Shows a /non-negative/ 'Integral' number using the base specified by the
+-- first argument, and the character representation specified by the second.
+showIntAtBase :: Integral a => a -> (Int -> Char) -> a -> ShowS
+showIntAtBase base toChr n0 r0
+ | base <= 1 = error ("Numeric.showIntAtBase: applied to unsupported base " ++ show base)
+ | n0 < 0 = error ("Numeric.showIntAtBase: applied to negative number " ++ show n0)
+ | otherwise = showIt (quotRem n0 base) r0
+ where
+ showIt (n,d) r = seq c $ -- stricter than necessary
+ case n of
+ 0 -> r'
+ _ -> showIt (quotRem n base) r'
+ where
+ c = toChr (fromIntegral d)
+ r' = c : r
+
+-- | Show /non-negative/ 'Integral' numbers in base 16.
+showHex :: Integral a => a -> ShowS
+showHex = showIntAtBase 16 intToDigit
+
+-- | Show /non-negative/ 'Integral' numbers in base 8.
+showOct :: Integral a => a -> ShowS
+showOct = showIntAtBase 8 intToDigit