this post was submitted on 20 Jun 2023
4 points (100.0% liked)

Haskell

65 readers
3 users here now

**The Haskell programming language community.** Daily news and info about all things Haskell related: practical stuff, theory, types, libraries, jobs, patches, releases, events and conferences and more... ### Links - Get Started with Haskell

founded 1 year ago
4
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 

The containers package contains IntMap and IntSet types which use some bit fiddling tricks to achieve very high performance.

For example -x xor x. This bit fiddling trick is especially magical because it negates an unsigned value. It would be much nicer to express the meaning of this operation in a more natural way.

One possibility I could think of would be a regex-inspired of pattern matching mechanism which could look like this:

.*10{n} -> 1*0{n+1}

This syntax is supposed to mean: "Check if the input ends in a one followed by a number n zeroes, if so produce a bitstring with all ones of the left and n+1 zeroes.

I think this is much easier to understand than the original which uses xor, but it doesn't quite feel perfect yet.

What do you think? Would you use this if it compiled to code that is as fast as the -x xor x? Do you have suggestions for improvement?

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago

I guess you could express it more Haskelly like:

padL 64 1 (cons 1 (takeWhileR (== 0) x))

(if we consider bitstrings to be Seq Bit and I hope that padL speaks for itself)

Would that be less confusing? Or can you try to explain what you find confusing?