Haskell 枚举器,奇怪的错误
我试图弄清楚枚举器是如何工作的,因此测试枚举器库。我有一个在我的台式计算机上编译的代码片段,但抱怨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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将以下导入更改为:
您
可能安装了旧版本的 mtl,因此 Control.Monad.Trans 和 Data.Enumerator 之间有不同的 MonadIO 类型类。
Try changing the import of:
to
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.