Haskell IO代码没有进行类型检查

时间:2010-11-09 03:05:48

标签: haskell io monads 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 = ...........
在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 }

1 个答案:

答案 0 :(得分:9)

map parsePerson lines的类型为[Person],但由于您需要loadPeople的结果类型为IO [Person],因此您需要使用{{IO将其包装在return中1}}:

return $ map parsePerson lines