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
[...]
ここで何が起こっているのですか?最初のバージョンは機能するのに、2 番目のバージョンは機能しないのはなぜですか?
Stack Overflow には、このような類似の質問がいくつかあることは知っていますが、この問題を説明しているようには見えませんでした。