[Haskell-Cafe] Parsing bytestream

Vo Minh Thu noteed at gmail.com
Tue Nov 9 05:22:00 EST 2010


2010/11/9 C K Kashyap <ckkashyap at gmail.com>:
> 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?

Since you want the result as a list, by somehow mapping over a list of
action, and since lists are homogeneous in Haskell, you need each
action to have the same result type. Something like:

performAction :: MyAction -> Get Int
performAction A1 = getWord8 >>= return . someHypotheticalWordToIntYouWouldLike

Notice that I have rewritten your

data MyAction = A1 (Get Word8)

as simply

data MyAction = A1

because it seems your A1 action will always be getWord8.

Now, you don't need specifically a list of MyAction and map
performAction on it: you can achieve the same thing with:

sequence [getWord8 >>= return . someHypotheticalWordToIntYouWouldLike, ...]

Cheers,
Thu


More information about the Haskell-Cafe mailing list