gpt4 book ai didi

haskell - 我如何在 Parsec 的 monad 中执行 IO?

转载 作者:行者123 更新时间:2023-12-05 02:23:33 25 4
gpt4 key购买 nike

我正在使用 Parsec 读取包含 FilePath 的简单文件到其他图像。

例如。

img ../images/test.gif
img ../../gifs/image.png

我想一次解析每一行,将图像作为 ByteString 读入,然后将其包装在 Parsec 的 monad 中返回。然而,一个看起来像这样的函数:

filename <- getName
contents <- BS.readFile fileName
results <- decodeImage contents
let image = case results of
Left err -> error $ show err
Right img -> img
return results

抛出错误

Couldn't match type `IO' with `ParsecT s0 u0 m0'
Expected type: ParsecT s0 u0 m0 BS.ByteString
Actual type: IO BS.ByteString

我还不确定 monad 是如何工作的——但它似乎将它包装在错误的 monad 中?有什么办法可以明确说明这一点?

最佳答案

您需要使用 liftIOmtl 包中的 Control.Monad.TransIO 上的操作转换为 ParsecT s0 u0 IO :

contents <- liftIO $ BS.readFile fileName
results <- liftIO $ decodeImage contents

关于haskell - 我如何在 Parsec 的 monad 中执行 IO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21180309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com