Haskell:为什么我不能使用liftM线.getContents

lin*_*aon 2 haskell

我有getLinesIn = liftM lines . getContents

readAndWriteIn = do
  list <- getLinesIn
Run Code Online (Sandbox Code Playgroud)

它不起作用.它说:无法匹配预期类型a0 - > m0字符串与实际类型IO字符串.我不明白为什么会这样?当我使用getLinesFile = liftM lines . readFile 它工作正常.我需要对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
Run Code Online (Sandbox Code Playgroud)

Lee*_*Lee 8

readFile是一个功能FilePath -> IO String,getContents只是IO String因此您不能使用(.)运算符来组合它liftM lines.你应该使用

getLinesIn = liftM lines getContents
Run Code Online (Sandbox Code Playgroud)

要么

getLinesIn = fmap lines getContents
Run Code Online (Sandbox Code Playgroud)