I am trying to parse a simple binary file in Haskell with the Data.Binary.Get monad.
A simplified version of my code looks like this:
data MsgString = Definition_msg {
msg_no :: Word16
} deriving (Show)
parseDef :: Get MsgString
parseDef = do
msg_no <- getWord16le
return $ Definition_msg msg_no
parseMain :: Get [MsgString]
parseMain = do
bit <- getWord8
msg <- parseDef
return msg:parseMain
And the error I get is the following:
Prelude> :l example.hs
[1 of 1] Compiling Main ( example.hs, interpreted )
example.hs:23:17:
Couldn't match expected type `[m MsgString]'
against inferred type `Get [MsgString]'
In the second argument of `(:)', namely `parseMain'
In the expression: return msg : parseMain
In the expression:
do { bit <- getWord8;
msg <- parseDef;
return msg : parseMain }
Failed, modules loaded: none.
Can anyone see what I do wrong?
Thanks!
The issue is your last line, which parses as:
But that really isn’t the only problem. parseMain is of type
Get [MsgString]when you really just want a[MsgString]so you must run the monadic action first:Notice this will get an infinite list of
MsgString‘s and won’t terminate without an exception. Perhaps you intended to have an if statement guarding that recursive call toparseMain?