Proposal for a generic showIntAtBase

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

On Sun, 5 Apr 2015, Vikas Menon wrote:
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.
That is, you want to decompose a number into digits of an arbitrary base? I would not associate that with "show". I have implemented this in some flavors: https://hackage.haskell.org/package/numeric-prelude-0.4.2/docs/Algebra-Integ... decomposePositional in http://code.haskell.org/~thielema/htam/src/NumberTheory.hs

Decomposing numbers into arbitrary bases *already* exists as showIntAtBase. Vikas's proposal is about generalizing this existing function to output more than just strings and characters. Whether the existing naming scheme makes sense is a separate issue. (The whole Numeric module is weird, from the non-hierarchical name through function like showFFloatAlt.) On Mon, Apr 6, 2015 at 3:35 AM, Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Sun, 5 Apr 2015, Vikas Menon wrote:
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.
That is, you want to decompose a number into digits of an arbitrary base? I would not associate that with "show". I have implemented this in some flavors: https://hackage.haskell.org/package/numeric-prelude-0.4.2/ docs/Algebra-IntegralDomain.html#v:decomposeVarPositional decomposePositional in http://code.haskell.org/~thielema/htam/src/NumberTheory.hs _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On Mon, 6 Apr 2015, Tikhon Jelvis wrote:
Decomposing numbers into arbitrary bases *already* exists as showIntAtBase. Vikas's proposal is about generalizing this existing function to output more than just strings and characters.
With "digits" I meant something different from "Char", e.g. Int. If this is the goal of the proposal then Vikas could use the implementations I pointed to, for now. I do not see a need to have the function in "base".

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
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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On Mon, 6 Apr 2015, Jon Purdy wrote:
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.
E.g. you can use it to pack a tuple of small integers space efficiently into a larger integer type. E.g. you can pack 3 integers from the range 0..4 into 7 bits using a base-5 representation, but you would need 9 bits if you reserve three bits for every small integer. Using the positional representation you can also convert between tupled and linear indexes of an array with equal extents in all dimensions (i.e. a hypercube). Both examples also work with mixed-base representations.

The idea is the same function can deal with Chars, "digits" or any other types (UTF16 for instance). This would subsume the current implementation of decomposeVarPositional and deal with many more. On Mon, Apr 6, 2015 at 3:04 PM, Henning Thielemann < lemming@henning-thielemann.de> wrote:
On Mon, 6 Apr 2015, Jon Purdy wrote:
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.
E.g. you can use it to pack a tuple of small integers space efficiently into a larger integer type. E.g. you can pack 3 integers from the range 0..4 into 7 bits using a base-5 representation, but you would need 9 bits if you reserve three bits for every small integer.
Using the positional representation you can also convert between tupled and linear indexes of an array with equal extents in all dimensions (i.e. a hypercube).
Both examples also work with mixed-base representations.

Vikas Menon
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]
This has nothing to do with show. And it's already there h> fst (floatToDigits 26 500) [19,6] -- lelf

This has nothing to do with show.
And it's already there
h> fst (floatToDigits 26 500) [19,6]
Oh, wait. Scratch that. You have to (++ (mantissa digits)), and it's only for RealFloat's.
Example: λ> showIntAtBaseGeneric 26 id (:) 500 [] -- convert 500 to base26 and provide output as a list. [19,6]
But what's wrong with just :: Integral a => Int -> a -> [Int] ?
participants (5)
-
Antonio Nikishaev
-
Henning Thielemann
-
Jon Purdy
-
Tikhon Jelvis
-
Vikas Menon