[Haskell-Cafe] Parsing bytestream

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Nov 9 05:23:21 EST 2010


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.


More information about the Haskell-Cafe mailing list