[Haskell-cafe] convert a list of booleans into Word*

Paul.Brauner at loria.fr Paul.Brauner at loria.fr
Wed Sep 30 05:18:03 EDT 2009


I haven't found a function in hackage or in the standard library that
takes a list of booleans (or a list of 0s and 1s, or a tuple of booleans
or 0s and 1s) and outputs a Word8 or Word32.

I have written one which seems very inefficient :

toWord8 :: [Bool] -> Word8
toWord8 bs = go 0 0 bs
  where go n r []     = r
        go n r (b:bs) = go (n+1) (if b then setBit r n else clearBit r n) bs

Is there a better way to do this out there ?

(If it helps, i'm writting a toy compression algorithm, which outputs
binary as lists of booleans, and I'd like to output that in a file).


More information about the Haskell-Cafe mailing list