开发者

Haskell IO code doesn't typecheck

I'm a beginner with Haskell and am having trouble figuring out some code. What do I need to do to get the types right on this IO section of my code?

Thanks in advance.

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 = ...........

map is underlined in red in Leksah, and the compile error I am receiving is:

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 <开发者_Python百科;- getLines file;
                            map parsePerson lines }


map parsePerson lines has type [Person], but since you need the result type of loadPeople is IO [Person], you need to wrap it in IO using return:

return $ map parsePerson lines
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜