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

2009-09-30 Thread Henning Thielemann
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

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

2009-09-30 Thread Eduard Sergeev
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. __

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

2009-09-30 Thread Aai
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

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

2009-09-30 Thread Eugene Kirpichov
...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

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

2009-09-30 Thread 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 of 0s and 1s, or a tuple of booleans >> or 0s and 1s) and outputs a Word8 or Word32. > > s

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

2009-09-30 Thread Bulat Ziganshin
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..])

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

2009-09-30 Thread Paul . Brauner
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

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

2009-09-30 Thread Aai
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

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

2009-09-30 Thread Aai
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

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

2009-09-30 Thread Roel van Dijk
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

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

2009-09-30 Thread Andrew Coppin
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

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

2009-09-30 Thread Paul . Brauner
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