gpt4 book ai didi

haskell IO : Couldn't match expected type `IO a0' with actual type

转载 作者:行者123 更新时间:2023-12-03 14:48:55 29 4
gpt4 key购买 nike

我是 Haskell 的新手,我尝试了解如何正确执行 IO。

以下工作正常:

main = do
action <- cmdParser
putStrLn "Username to add to the password manager:"
username <- getLine
case action of
Add -> persist entry
where
entry = Entry username "somepassword"

而以下导致编译错误:
main = do
action <- cmdParser
case action of
Add -> persist entry
where
entry = Entry promptUsername "somepassword"

promptUsername = do
putStrLn "Username to add to the password manager:"
username <- getLine

错误在这里:
Couldn't match expected type `IO b0' with actual type `[Char]'
Expected type: IO b0
Actual type: String
In the expression: username
[...]

这里发生了什么?为什么第一个版本有效,而第二个版本无效?

我知道在 Stack Overflow 中有一些类似的问题,但似乎没有一个可以向我解释这个问题。

最佳答案

usernameString , 但是 promptUsernameIO String .您需要执行以下操作:

username <- promptUsername
let entry = Entry username "somepassword"
persist entry

关于 haskell IO : Couldn't match expected type `IO a0' with actual type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016407/

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