Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Convert bits to bytes (Alec Benzer)
   2. Re:  Convert bits to bytes (Alec Benzer)
   3. Re:  Missing or recursive dependencies (Daniel Fischer)
   4. Re:  Missing or recursive dependencies (Lyndon Maydwell)
   5. Re:  Convert bits to bytes (Daniel Fischer)
   6.  question on typeclasses and applicatives (Alec Benzer)
   7. Re:  question on typeclasses and applicatives (Daniel Fischer)


----------------------------------------------------------------------

Message: 1
Date: Thu, 2 Sep 2010 13:29:23 -0400
From: Alec Benzer <[email protected]>
Subject: Re: [Haskell-beginners] Convert bits to bytes
To: David Virebayre <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Thanks - I thought there would be some api for it but I see it's pretty
simple to do on your own.

I came up with this (dealing with non-multiple of 8 numbers of bits)

bitsToBytes :: [Bool] -> [Word8]
bitsToBytes [] = []
bitsToBytes bits = map bitsToByte (getChunks bits)
  where bitsToByte = foldl' (\byte bit -> byte*2 + if bit then 1 else 0) 0

getChunks :: [Bool] -> [[Bool]]
getChunks [] = []
getChunks xs
  | length xs < 8 = getChunks $ take 8 (xs ++ repeat False)
  | otherwise =
    let (these,rest) = splitAt 8 xs
    in these:getChunks rest


On Thu, Sep 2, 2010 at 3:18 AM, David Virebayre
<[email protected]<dav.vire%[email protected]>
> wrote:

> I could have been a bit more verbose.
>
> The way I see it, since you have an arbitrary long list of 'bits' that
> you want to convert into bytes, the first thing to do is to group this
> list into sublists of 8 bits.
>
> That's what chunk does: it splits the list at the 8th element, and
> recursively does it for the rest of the list, until the list is empty.
>
> One problem with that is that if the length of the list isn't a
> multiple of 8, then the last byte might be incorrect.
>
> > chunk :: [Bool] -> [[ Bool ]]
> > chunk [] = []
> > chunk l = a : chunk b
> >  where (a,b)  = splitAt 8 l
>
> This one converts a list of 'bits' into a number. The head of the list
> is assumed to be the most significant bit :
>
> > conv1 = foldl' (\a b -> a*2 + if b then 1 else 0) 0
>
> if we want the head of the list to be the least significant bit, then
> you can convert with foldr :
>
> > conv1' = foldr (\b a -> a*2 + if b then 1 else 0) 0
>
> Now converting the whole list is just a matter converting the whole
> list in groups, then converting each group :
>
> > convlist = map conv1 . chunk
> >
> > test = convlist (replicate 8 True ++ replicate 8 False :: [Bool] )
>
>
> David.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100902/a62f3fc9/attachment-0001.html

------------------------------

Message: 2
Date: Thu, 2 Sep 2010 13:30:51 -0400
From: Alec Benzer <[email protected]>
Subject: Re: [Haskell-beginners] Convert bits to bytes
To: David Virebayre <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Whoops, realized bitsToBytes [] = [] wasn't necessary.

On Thu, Sep 2, 2010 at 1:29 PM, Alec Benzer <[email protected]> wrote:

> Thanks - I thought there would be some api for it but I see it's pretty
> simple to do on your own.
>
> I came up with this (dealing with non-multiple of 8 numbers of bits)
>
> bitsToBytes :: [Bool] -> [Word8]
> bitsToBytes [] = []
> bitsToBytes bits = map bitsToByte (getChunks bits)
>   where bitsToByte = foldl' (\byte bit -> byte*2 + if bit then 1 else 0) 0
>
> getChunks :: [Bool] -> [[Bool]]
> getChunks [] = []
> getChunks xs
>   | length xs < 8 = getChunks $ take 8 (xs ++ repeat False)
>   | otherwise =
>     let (these,rest) = splitAt 8 xs
>     in these:getChunks rest
>
>
> On Thu, Sep 2, 2010 at 3:18 AM, David Virebayre <
> [email protected] <dav.vire%[email protected]>> wrote:
>
>> I could have been a bit more verbose.
>>
>> The way I see it, since you have an arbitrary long list of 'bits' that
>> you want to convert into bytes, the first thing to do is to group this
>> list into sublists of 8 bits.
>>
>> That's what chunk does: it splits the list at the 8th element, and
>> recursively does it for the rest of the list, until the list is empty.
>>
>> One problem with that is that if the length of the list isn't a
>> multiple of 8, then the last byte might be incorrect.
>>
>> > chunk :: [Bool] -> [[ Bool ]]
>> > chunk [] = []
>> > chunk l = a : chunk b
>> >  where (a,b)  = splitAt 8 l
>>
>> This one converts a list of 'bits' into a number. The head of the list
>> is assumed to be the most significant bit :
>>
>> > conv1 = foldl' (\a b -> a*2 + if b then 1 else 0) 0
>>
>> if we want the head of the list to be the least significant bit, then
>> you can convert with foldr :
>>
>> > conv1' = foldr (\b a -> a*2 + if b then 1 else 0) 0
>>
>> Now converting the whole list is just a matter converting the whole
>> list in groups, then converting each group :
>>
>> > convlist = map conv1 . chunk
>> >
>> > test = convlist (replicate 8 True ++ replicate 8 False :: [Bool] )
>>
>>
>> David.
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100902/0bee2fe7/attachment-0001.html

------------------------------

Message: 3
Date: Thu, 2 Sep 2010 19:34:27 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Missing or recursive dependencies
To: Lyndon Maydwell <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 02 September 2010 18:28:09, Lyndon Maydwell wrote:
> I tried it, and it seems to have cleared up the error.

Unregister tha Cabal in the user-db, I presume. ghc-6.12.3 depends on the 
global one, as that's the one built together with the compiler.
Generally, unregistering stuff that came with the compiler itself is a bad 
idea.


------------------------------

Message: 4
Date: Fri, 3 Sep 2010 01:44:01 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] Missing or recursive dependencies
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Thanks for the help!


------------------------------

Message: 5
Date: Thu, 2 Sep 2010 19:44:44 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Convert bits to bytes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 02 September 2010 19:29:23, Alec Benzer wrote:
> I came up with this (dealing with non-multiple of 8 numbers of bits)
>
> bitsToBytes :: [Bool] -> [Word8]
> bitsToBytes [] = []
> bitsToBytes bits = map bitsToByte (getChunks bits)
>   where bitsToByte = foldl' (\byte bit -> byte*2 + if bit then 1 else 0)
> 0
>
> getChunks :: [Bool] -> [[Bool]]
> getChunks [] = []
> getChunks xs
>   | length xs < 8 = getChunks $ take 8 (xs ++ repeat False)

Pet peeve.
Don't use `length xs < k' (or <=, ==, >=, >).
That fails hard on infinite lists, also it is slow and prone to cause a 
space leak on long lists.
Remember, length must walk the entire list.
Instead of length xs < k use null (drop (k-1) xs), other combinations of 
not, null and drop for the other tests.
Or you can use lazy Peano numbers and check

genericLength xs < (8 :: Peano).

>   | otherwise =
>     let (these,rest) = splitAt 8 xs
>     in these:getChunks rest



------------------------------

Message: 6
Date: Thu, 2 Sep 2010 15:02:33 -0400
From: Alec Benzer <[email protected]>
Subject: [Haskell-beginners] question on typeclasses and applicatives
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I came up with some code when trying to understand applicatives:

import Control.Applicative
import qualified Data.Map as M

instance Applicative (M.Map String) where
  pure x = M.fromList [("",x)]
  fs <*> xs = M.fromList [(k1 ++ " " ++ k2,v1 v2) | k1 <- M.keys fs, k2 <-
M.keys xs, v1 <- M.elems fs, v2 <- M.elems xs]

1. When I :load this in ghci it gives me some error about using (M.Map
String) here, and tells me it'll work if I use the -XFlexibleInstances flag.
Why is this type of behavior disabled by default? Is it potentially
dangerous in some way?

2. When running the following:

       fromList [("double",(*2))] <*> fromList[("two",2),("seven",7)]

I get:

        fromList [("double seven",4),("double two",4)]

instead of what I'd expect:

        fromList [("double seven",14),("double two",4)]

Although this:

        (*2) <$> fromList[("two",2),("seven",7)]

gives what I'd expect:

        fromList [("seven",14),("two",4)]

Why is this happening? I can't seem to figure it out.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100902/f0974c8f/attachment-0001.html

------------------------------

Message: 7
Date: Thu, 2 Sep 2010 21:19:58 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] question on typeclasses and
        applicatives
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Thursday 02 September 2010 21:02:33, Alec Benzer wrote:
> I came up with some code when trying to understand applicatives:
>
> import Control.Applicative
> import qualified Data.Map as M
>
> instance Applicative (M.Map String) where
>   pure x = M.fromList [("",x)]
>   fs <*> xs = M.fromList [(k1 ++ " " ++ k2,v1 v2) | k1 <- M.keys fs, k2
> <- M.keys xs, v1 <- M.elems fs, v2 <- M.elems xs]
>
> 1. When I :load this in ghci it gives me some error about using (M.Map
> String) here, and tells me it'll work if I use the -XFlexibleInstances
> flag. Why is this type of behavior disabled by default?

Because the language specification imposed that instance declarations must 
have the form

instance Class (T a1 a2 ... an) where ...

where T is a type constructor, 0 <= n and a1, a2, ..., an are *distinct* 
type variables.

> Is it potentially dangerous in some way?

I know of no dangers off the top of my head.

>
> 2. When running the following:
>
>        fromList [("double",(*2))] <*> fromList[("two",2),("seven",7)]
>
> I get:
>
>         fromList [("double seven",4),("double two",4)]
>
> instead of what I'd expect:
>
>         fromList [("double seven",14),("double two",4)]

That's because you really get

fromList [("double seven", (*2) 7),("double seven", (*2) 2), ("double two", 
(*2) 7), ("double two", (*2) 2)]

and later values for the same key overwrite earlier.

You probably wanted

  fs <*> xs = M.fromList [(k1 ++ " " ++ k2, v1 v2) | (k1,v1) <- M.assocs 
fs, (k2,v2) <- M.assocs xs]

>
> Although this:
>
>         (*2) <$> fromList[("two",2),("seven",7)]
>
> gives what I'd expect:
>
>         fromList [("seven",14),("two",4)]
>
> Why is this happening? I can't seem to figure it out.



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 5
****************************************

Reply via email to