Haskell:为什么我不能使用 liftM 线。获取内容

标签 haskell

我有 getLinesIn = liftM lines 。 getContents

readAndWriteIn = do
  list <- getLinesIn

它不起作用。 它说:无法将预期类型 a0 -> m0 String 与实际类型 IO String 匹配。 我不明白这是为什么? 当我使用 getLinesFile = liftM lines 时。读取文件 它工作正常。 我需要对 getContents 做同样的事情。有办法吗?

感谢您的任何想法。

编辑: 完整输出:

Couldn't match expected type `a0 -> m0 String'
            with actual type `IO String'
In the second argument of `(.)', namely `getContents'
In the expression: liftM lines . getContents
In an equation for `getLinesIn':
    getLinesIn = liftM lines . getContents

最佳答案

readFile 是一个函数 FilePath -> IO StringgetContents 只是 IO String 所以你不能使用(.) 运算符将其与 liftM 行 组合在一起。你应该只使用

getLinesIn = liftM lines getContents

getLinesIn = fmap lines getContents

关于Haskell:为什么我不能使用 liftM 线。获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450697/

相关文章:

haskell - 组合任一类型变量

haskell - 如何处理我的类型中的用户插件?

haskell - 变量的Lambda演算变化及应用问题

haskell - 使用类型编号在 Haskell 中生成给定数量的函数

design-patterns - 如何将仅缓存某些内容的字段添加到 ADT?

haskell - 比较两个字符串列表并删除部分匹配项

haskell - 使用 constructive-algebra 包定义代数模块

haskell - 线程在 MVar 操作中无限期阻塞

algorithm - 尝试优化此算法/代码以计算 a 和 b 之间互质整数的最大子集

haskell - 关于懒惰的困惑