【发布时间】:2023-04-03 18:43:01
【问题描述】:
我是 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 = ...........
map 在 Leksah 中带有红色下划线,我收到的编译错误是:
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 }
【问题讨论】:
-
您可以使用 Functor 消除“getLines”函数(请参阅:learnyouahaskell.com/functors-applicative-functors-and-monoids)。你可以这样做:“ls
标签: haskell io monads typechecking