gpt4 book ai didi

Haskell IO 代码不进行类型检查

转载 作者:行者123 更新时间:2023-12-02 18:39:07 25 4
gpt4 key购买 nike

我是 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 }

最佳答案

map parsePersonlines 的类型为 [Person],但由于您需要 loadPeople 的结果类型为 IO [Person] ,您需要使用return将其包装在IO中:

return $ map parsePerson lines

关于Haskell IO 代码不进行类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4130153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com