在 Haskell 中使用 Data.Binary.Get 解析简单的二进制文件

发布于 2024-12-11 19:00:05 字数 978 浏览 0 评论 0原文

我正在尝试使用 Data.Binary.Get monad 解析 Haskell 中的一个简单的二进制文件。

我的代码的简化版本如下所示:

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

我得到的错误如下:

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.

任何人都可以看到我做错了什么吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-12-18 19:00:05

问题是你的最后一行,它解析为:

(return msg) : parseMain

但这确实不是唯一的问题。当您确实只想要一个 [MsgString] 时,parseMain 的类型为 Get [MsgString],因此您必须首先运行单子操作:

parseMain :: Get [MsgString]
parseMain =  do
    bit <- getWord8
    msg <- parseDef
    rest <- parseMain
    return (msg : rest)

注意,这将获得 的无限列表>MsgString's 并且不会在没有异常的情况下终止。也许您打算使用 if 语句来保护对 parseMain 的递归调用?

The issue is your last line, which parses as:

(return msg) : parseMain

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:

parseMain :: Get [MsgString]
parseMain =  do
    bit <- getWord8
    msg <- parseDef
    rest <- parseMain
    return (msg : rest)

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 to parseMain?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文