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

Aai bradypus at xs4all.nl
Wed Sep 30 07:02:42 EDT 2009


Here's another approach for Bool lists with msb leftmost:

bitsToInt :: [Bool] -> Integer
bitsToInt = foldr((.(flip shiftL 1)).(+)) 0. map (fromIntegral.fromEnum)



Hallo Paul.Brauner at loria.fr, je schreef op 30-09-09 11:18:
> Hello,
>
> 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).
>
> Cheers
> Paul
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>   

-- 
Met vriendelijke groet,
=@@i



More information about the Haskell-Cafe mailing list