Ale*_*sky 3 io monads haskell typechecking
我是Haskell的初学者,我很难搞清楚一些代码.我需要做什么才能在我的代码的IO部分获得正确的类型?
提前致谢.
loadPeople :: FilePath -> IO [Person]
loadPeople file = do
lines <- getLines file
map parsePerson lines
getLines :: FilePath -> IO [String]
getLines = liftM lines . readFile
parsePerson :: String -> Person
parsePerson line = ...........
Run Code Online (Sandbox Code Playgroud)
map 在Leksah中用红色加下划线,我收到的编译错误是:
src\Main.hs:13:3:
Couldn't match expected type `IO [Person]'
against inferred type `[Person]'
In the expression: map parsePerson lines
In the expression:
do { lines <- getLines file;
map parsePerson lines }
In the definition of `loadPeople':
loadPeople file
= do { lines <- getLines file;
map parsePerson lines }
Run Code Online (Sandbox Code Playgroud)
map parsePerson lines有类型[Person],但由于你需要结果类型loadPeople是IO [Person],你需要将其包装IO使用return:
return $ map parsePerson lines
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
223 次 |
| 最近记录: |