Proposal for a generic showIntAtBase

Jon Purdy evincarofautumn at gmail.com
Mon Apr 6 18:51:32 UTC 2015


Could you offer a specific use case? When I need to convert a number
to its representation in some base, it’s because I’m displaying or
serialising it. A base is a notation (or a variant of a notation) and
it seems sensible to represent such notations with characters.

This raises another question: why is the base parameter overloaded?
base’s implementation of showIntAtBase uses quotRem on the base and
the number, but it could easily cast the base from Int. I imagine the
answer is “historical accident”.

On Sun, Apr 5, 2015 at 9:46 AM, Vikas Menon <menonvikas at gmail.com> wrote:
>
> Hi,
>
> 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
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>


More information about the Libraries mailing list