Haskell IO Code Does Not Type Check

I start with Haskell, and it's hard for me to understand some kind of code. What do I need to do to get the types in this I / O 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 compilation error that I get:

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 }
+3
source share
1 answer

map parsePerson lineshas a type [Person], but since you need the result type loadPeopleis IO [Person], you need to wrap it in IOwith return:

return $ map parsePerson lines
+9
source

Source: https://habr.com/ru/post/1773752/


All Articles