无法与实际类型IO()匹配的预期类型[a0] [英] couldn't match expected type [a0] with actual type IO ()
本文介绍了无法与实际类型IO()匹配的预期类型[a0]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码有什么问题:
insertValue file x =
if x ==10then ok
else putStrLnError;文件
解决方案
您需要使用 return :: a - > IO a
将您的字符串提升为 IO字符串
:
insertValue file x =
if x ==10
然后返回ok
else putStrLnError
返回文件
但是您确定不想调用 putStrLnok
(而不是 returnok
)并返回一个Maybe值?否则,您将返回文件
或ok
,调用者无法确定调用<$时是否有错误c $ c> insertValue 放在名为ok的文件上。
What is wrong in my code:
insertValue file x =
if x == "10" then "ok"
else do putStrLn "Error"; file
解决方案
You need to use return :: a -> IO a
to "lift" your strings into IO String
:
insertValue file x =
if x == "10"
then return "ok"
else do putStrLn "Error"
return file
But are you sure you don't want to call putStrLn "ok"
(instead of return "ok"
) and return a Maybe value? Otherwise you are returning file
or "ok"
and your caller could never determine if there was an error when calling insertValue
on a file named "ok".
这篇关于无法与实际类型IO()匹配的预期类型[a0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文