[Haskell-beginners] Convert bits to bytes

David Virebayre dav.vire+haskell at gmail.com
Thu Sep 2 03:07:20 EDT 2010


2010/9/2 David Virebayre <dav.vire+haskell at gmail.com>:
> Simple way :
>
> l = [False,False,False,True,False,False,True]
> foldl' (\a b -> a*2 + if b then 1 else 0) 0 l

Oh sorry I answered too quickly and didn't see the list can have more
than 8 bits.


chunk :: [Bool] -> [[ Bool ]]
chunk [] = []
chunk l = a : chunk b
  where (a,b)  = splitAt 8 l

conv1 = foldl' (\a b -> a*2 + if b then 1 else 0) 0

convlist = map conv1 . chunk

test = convlist (replicate 8 True ++ replicate 8 False :: [Bool] )


More information about the Beginners mailing list