The current implementation of showIntAtBase in Numeric is limited to Chars currently.
showIntAtBase
:: (Integral a, Show a) => a -> (Int -> Char) -> a -> ShowS
The 2 reasons for this constraint is:
a) We only accept functions of the form (Int -> Char)
b) An implicit concatenation using (:)
I'd like to propose a new function showIntAtBaseGeneric that removes the (Int -> Char) function constraint and takes an additional function to replace the implicit (:) operator.
showIntAtBaseGeneric
:: (Integral a1, Num b, Show a1) =>
a1 -> (b -> a) -> (a -> s -> s) -> a1 -> s -> s
Now showIntAtBase may be implemented as:
showIntAtBase :: (Integral a, Show a) => a -> (Int -> Char) -> a -> ShowS
showIntAtBase base toChr n0 r0 = showIntAtBaseGeneric base toChr (:) n0 r0
The API and behavior of showIntAtBase remains unchanged while allowing for generic conversions not limited to Chars alone.
Example:
λ> showIntAtBaseGeneric 26 id (:) 500 [] -- convert 500 to base26 and provide output as a list.
[19,6]
Please let me know thoughts/concerns if any to this proposal.
Cheers,
Vikas