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
精彩评论