digit groups

Iavor Diatchki iavor.diatchki at gmail.com
Thu Oct 26 14:41:37 EDT 2006


On 10/25/06, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:
> Hello Iavor,
> Thursday, October 26, 2006, 4:51:00 AM, you wrote:
> > kb,mg,gb :: Num a => a
> > kb = 1024
> > mb = 1024 * kb
> > gb = 1024 * mb
> b :kb :mb :gb :_       = iterate (1024*) 1 :: [Int]
> b_:kb_:mb_:gb_:tb_:_   = iterate (1024*) 1 :: [Integer]
> > and now we can write (4 * kb) instead for 4096.
> btw, your variant requires re-calculating values on each their use

There are other integral types for which one might use these constants
(e.g., Word16 and Word32), so the overloading is useful.  The
multiplications might be repeated depending on the compipler but, as
other people pointed out, this is a fairly easy optimization to
perform and GHC probably does it.  The overloading might make things a
little more tricky, but it should be still doable (it is the same
problem as turning 1+2 into 3 at compiple time).

On the other hand, none of these are important, if we were to use the
literal notation that I suggested.  Then, the multiplications would
happen in the lexer when we compute the value for the literal.  I
think that the main advantage of the literals is that they are more
readable.  For example, an editor can color them in the same way as it
colors other literals.


More information about the Haskell-prime mailing list