【问题标题】:Using lookup with an IO list?使用带有 IO 列表的查找?
【发布时间】:2012-10-17 08:52:05
【问题描述】:

我正在获取文件的内容并将其转换为表单列表:

[("abc", 123), ("def", 456)]

带有 readFile、行和单词。

现在,我可以设法将结果列表转换为类型 IO [(String, Int)]

我的问题是,当我尝试制作这样的功能时:

check x = lookup x theMap

我收到此错误,我不太确定如何解决:

Couldn't match expected type `[(a0, b0)]'
            with actual type `IO [(String, Int)]'
In the second argument of `lookup', namely `theMap'

theMap 本质上是这样的:

getLines :: String -> IO [String]
getLines = liftM lines . readFile

tuplify [x,y] = (x, read y :: Int)

theMap = do
    list <- getLines "./test.txt"
    let l = map tuplify (map words list)
    return l

文件内容为:

abc 123
def 456

谁能解释我做错了什么,或者告诉我一个更好的解决方案?几个小时前我刚开始玩 monads,一路上遇到了一些颠簸。

谢谢

【问题讨论】:

    标签: haskell io monads


    【解决方案1】:

    您必须从 IO 中“解包”theMap。请注意您已经通过以下方式对getLines 执行此操作:

    do
      list <- getlines
      [...]
      return (some computation on list)
    

    所以你可以:

    check x = do
      m <- theMap
      return . lookup x $ m
    

    事实上,这是一种反模式(尽管只是说明性的),最好使用仿函数实例,即。 check x = fmap (lookup x) theMap

    【讨论】:

    • 谢谢!我应该注意到这一点:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2021-11-22
    • 2015-03-28
    相关资源
    最近更新 更多