我是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 = ...........
在Leksah中, map
以红色加下划线,我收到的编译错误是:
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 }
答案 0 :(得分:9)
map parsePerson lines
的类型为[Person]
,但由于您需要loadPeople
的结果类型为IO [Person]
,因此您需要使用{{IO
将其包装在return
中1}}:
return $ map parsePerson lines