sda*_*das 2 io monads haskell parsec
我正在使用Parsec来读取包含FilePaths到其他图像的简单文件.
例如.
img ../images/test.gif
img ../../gifs/image.png
Run Code Online (Sandbox Code Playgroud)
我想一次解析每一行,将图像作为a读取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
Run Code Online (Sandbox Code Playgroud)
抛出错误
Couldn't match type `IO' with `ParsecT s0 u0 m0'
Expected type: ParsecT s0 u0 m0 BS.ByteString
Actual type: IO BS.ByteString
Run Code Online (Sandbox Code Playgroud)
我不太确定monad是如何工作的 - 但似乎它将它包装在错误的monad中?有没有办法让我明白这一点?
您需要在包中使用liftIOfrom 来将操作转换为:Control.Monad.TransmtlIOParsecT s0 u0 IO
contents <- liftIO $ BS.readFile fileName
results <- liftIO $ decodeImage contents
Run Code Online (Sandbox Code Playgroud)