[Haskell-beginners] Convert bits to bytes

David Virebayre dav.vire+haskell at gmail.com
Thu Sep 2 02:56:12 EDT 2010


2010/9/2 Alec Benzer <alecbenzer at gmail.com>:
> What's the quickest/the best/a way of converting a series of bits into
> bytes? Ie, converting a [Bool] to a [Word8]. So if I had replicate 8 True ++
> replicate 8 False :: [Bool], it would spit out [255,0] :: [Word8].

Simple way :

l = [False,False,False,True,False,False,True]
foldl' (\a b -> a*2 + if b then 1 else 0) 0 l


More information about the Beginners mailing list