我有 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 String
而 getContents
只是 IO String
所以你不能使用(.)
运算符将其与 liftM 行
组合在一起。你应该只使用
getLinesIn = liftM lines getContents
或
getLinesIn = fmap lines getContents
关于Haskell:为什么我不能使用 liftM 线。获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450697/