On Wed, 30 Sep 2009, Jochem Berndsen wrote:
Bulat Ziganshin wrote:
Hello Paul,
Wednesday, September 30, 2009, 1:18:03 PM, you wrote:
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) a
Bulat Ziganshin-2 wrote:
>
> sum . zipWith (*) (map (2^) [0..])
>
foldr1 $ \b -> (+b) . (*2)
--
View this message in context:
http://www.nabble.com/convert-a-list-of-booleans-into-Word*-tp25677589p25686400.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
__
Very fast for long boolean lists by using a strict foldl and reversing
the input:
bsToInt :: [Bool] -> Integer
bsToInt = foldl' ((.fromIntegral.fromEnum).(+).join(+)) 0. reverse
Try this:
(>1) $ bsToInt $ take 10 $ cycle [True,True,False,True,True,False,True]
>> bitsToInt :: [Bool] -> Int
...Or let's fuse it.
sum . zipWith ((*).(2^)) [0..]
2009/9/30 Jochem Berndsen :
> Bulat Ziganshin wrote:
>> Hello Paul,
>>
>> Wednesday, September 30, 2009, 1:18:03 PM, you wrote:
>>
>>> I haven't found a function in hackage or in the standard library that
>>> takes a list of booleans (or a list o
Bulat Ziganshin wrote:
> Hello Paul,
>
> Wednesday, September 30, 2009, 1:18:03 PM, you wrote:
>
>> 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.
>
> s
Hello Paul,
Wednesday, September 30, 2009, 1:18:03 PM, you wrote:
> 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.
sum . zipWith (*) (map (2^) [0..])
Thanks for the answers. I already had a look at Binary but, as said
above, it doesn't support bit manipulation, only bytes.
On Wed, Sep 30, 2009 at 11:18:03AM +0200, paul.brau...@loria.fr wrote:
> Hello,
>
> I haven't found a function in hackage or in the standard library that
> takes a list of
Sorry, msb rigthmost
> Here's another approach for Bool lists with msb leftmost:
>
> bitsToInt :: [Bool] -> Integer
> bitsToInt = foldr((.(flip shiftL 1)).(+)) 0. map (fromIntegral.fromEnum)
>
>
>
> Hallo paul.brau...@loria.fr, je schreef op 30-09-09 11:18:
>
>> Hello,
>>
>> I haven't found a f
Here's another approach for Bool lists with msb leftmost:
bitsToInt :: [Bool] -> Integer
bitsToInt = foldr((.(flip shiftL 1)).(+)) 0. map (fromIntegral.fromEnum)
Hallo paul.brau...@loria.fr, je schreef op 30-09-09 11:18:
> Hello,
>
> I haven't found a function in hackage or in the standard libr
I wrote a few variants for fun. Probably equally inefficient. I
suggest you look at Data.Binary as Andrew suggested.
-- Your original function, but with a more generic type signature.
encodeBits :: Bits n => [Bool] -> n
encodeBits bs = go 0 0 bs
where
go n r [] = r
go n r (b:b
paul.brau...@loria.fr wrote:
(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).
By a strange coincidence, I did the self same thing a while back.
There is Data.Binary which supports efficient reading and
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
12 matches
Mail list logo