[Haskell-Cafe] Parsing bytestream

C K Kashyap ckkashyap at gmail.com
Mon Nov 15 11:05:47 EST 2010


Hi Felipe,


On Tue, Nov 9, 2010 at 3:53 PM, Felipe Almeida Lessa
<felipe.lessa at gmail.com> wrote:
> On Tue, Nov 9, 2010 at 8:10 AM, C K Kashyap <ckkashyap at gmail.com> wrote:
> ] I think I can restate my problem like this ---
> ]
> ] If I have a list of actions as follows -
> ]
> ] import Data.Word
> ] import Data.Binary.Get
> ]
> ] data MyAction = A1 (Get Word8) | A2 (Get Word16)
> ]
> ] a = A1 getWord8
> ] b = A2 getWord16be
> ]
> ] listOfActions = [a,b,a]
> ]
> ] How can I execute the "listOfActions" inside of a Get Monad and get
> ] the output as a list?
>
> Do you mean, something like this?
>
>> import Control.Applicative ((<$>))
>>
>> data MyAction m = A1 (m Word8) | A2 (m Word16)
>>
>> a = A1 getWord8
>> b = A2 getWord16be
>>
>> listOfActions = [a,b,a]
>>
>> newtype Id a = Id a
>>
>> getAction :: MyAction Get -> Get (MyAction Id)
>> getAction (A1 act) = A1 . Id <$> act
>> getAction (A2 act) = A2 . Id <$> act
>>
>> getActions :: [MyAction Get] -> Get [MyAction Id]
>> getActions = mapM getAction
>
> --
> Felipe.
>


Could you please give a solution for Put as well ... I need to
generate a series of put actions from a list of tuples as follows -

[(100,1),(200,2),500,4)] -> [putWord8 100, putWord16be 200, putWord32be 500]

-- 
Regards,
Kashyap


More information about the Haskell-Cafe mailing list