[Haskell-cafe] Binary constants in Haskell

Josef Svenningsson josef.svenningsson at gmail.com
Thu Oct 25 08:40:36 EDT 2007

On 10/24/07, Neil Mitchell <ndmitchell at gmail.com> wrote:
> Hi
> > > Are there binary constants in Haskell, as
> > > we have, for instance, 0o232 for octal and
> > > 0xD29A for hexadecimal?
> >
> > No, though it is an interesting idea.
> You can get pretty close with existing Haskell though:
> (bin 100010011)
> where bin :: Integer -> Integer, and is left as an exercise for the
> reader. Obviously its not as high performance, as proper binary
> literals, but if you write them as top-level constants, they'll only
> be computed once and shouldn't end up being in the performance
> critical bits.
To make it efficient you could use Template Haskell and have the bin
function generate the constant which could then be spliced in. I
suppose it would look something like:
$(bin 100010011)

Not too bad.


More information about the Haskell-Cafe mailing list