Haskell 枚举器,奇怪的错误

发布于 2024-10-17 05:52:02 字数 1327 浏览 1 评论 0原文

我试图弄清楚枚举器是如何工作的,因此测试枚举器库。我有一个在我的台式计算机上编译的代码片段,但抱怨No instance for MonadIO。我是否不知道如何使用枚举器库,或者我的笔记本电脑出了什么问题?

iterateetests.hs:29:17:
    No instance for (MonadIO (Iteratee Int IO))
      arising from a use of `enumeratorFile' at iterateetests.hs:29:17-32
    Possible fix:
      add an instance declaration for (MonadIO (Iteratee Int IO))
    In the first argument of `(==<<)', namely `enumeratorFile h'
    In the first argument of `run_', namely
        `(enumeratorFile h ==<< summer)'
    In the expression: run_ (enumeratorFile h ==<< summer)

和代码

import Data.Enumerator
import qualified Data.Enumerator.List as EL
import System.IO
import Control.Exception.Base
import Control.Monad.Trans

summer :: (Monad m) => Iteratee Int m Int
summer = do
  m <- EL.head
  case m of
       Nothing -> return 0
       Just i -> do
     rest <- summer
     return (i+rest)

enumeratorFile h (Continue k) = do
  e <- liftIO (hIsEOF h)
  if e
     then k EOF
     else do
       l <- liftIO $ hGetLine h
       k (Chunks [read l]) >>== enumeratorFile h
enumeratorFile _ step = returnI  step

main = do
  bracket
    (openFile "numberlist" ReadMode)
    (hClose)
    (\h -> run_ (enumeratorFile h ==<< summer))

I'm trying to figure out how enumerators work, and therefore testing the enumerator library. I have a snippet which compiles on my desktop computer, but complains about No instance for MonadIO. Am I way off on how to use the enumerator library or is something amiss with my laptop?

iterateetests.hs:29:17:
    No instance for (MonadIO (Iteratee Int IO))
      arising from a use of `enumeratorFile' at iterateetests.hs:29:17-32
    Possible fix:
      add an instance declaration for (MonadIO (Iteratee Int IO))
    In the first argument of `(==<<)', namely `enumeratorFile h'
    In the first argument of `run_', namely
        `(enumeratorFile h ==<< summer)'
    In the expression: run_ (enumeratorFile h ==<< summer)

And the code

import Data.Enumerator
import qualified Data.Enumerator.List as EL
import System.IO
import Control.Exception.Base
import Control.Monad.Trans

summer :: (Monad m) => Iteratee Int m Int
summer = do
  m <- EL.head
  case m of
       Nothing -> return 0
       Just i -> do
     rest <- summer
     return (i+rest)

enumeratorFile h (Continue k) = do
  e <- liftIO (hIsEOF h)
  if e
     then k EOF
     else do
       l <- liftIO $ hGetLine h
       k (Chunks [read l]) >>== enumeratorFile h
enumeratorFile _ step = returnI  step

main = do
  bracket
    (openFile "numberlist" ReadMode)
    (hClose)
    (\h -> run_ (enumeratorFile h ==<< summer))

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

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

发布评论

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

评论(1

折戟 2024-10-24 05:52:02

尝试将以下导入更改为:

import Control.Monad.Trans

import Control.Monad.IO.Class

可能安装了旧版本的 mtl,因此 Control.Monad.Trans 和 Data.Enumerator 之间有不同的 MonadIO 类型类。

Try changing the import of:

import Control.Monad.Trans

to

import Control.Monad.IO.Class

It may be that you have an older version of mtl installed, and therefore have different MonadIO typeclasses between Control.Monad.Trans and Data.Enumerator.

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