抱歉,如果我不能制作一个更好的标题。我正在尝试在 Haskell 中使用 monad,但遇到了一些麻烦。
所以,我应该做的是:
定义函数
repeat:: IO Bool -> IO () -> IO()
这样就repeat test oper可以重复oper直到条件test满足True
所以,我这样做了:
repeat:: IO Bool -> IO () -> IO()
repeat test oper
= do res <- test
if res
then return ()
else do oper
repeat test oper
Run Code Online (Sandbox Code Playgroud)
但这段代码不起作用。你能解释一下为什么吗?现在我收到“如果输入时解析错误”。我想这只是一个语法错误,但我仍然不知道如何解决这个练习。
如您所知,haskell 中的嵌套是通过使用空格来完成的。正确格式化你的代码,它就会工作。请记住在 haskell 中使用空格而不是制表符,因为制表符通常会导致奇怪的解析错误。
repeat :: IO Bool -> IO () -> IO()
repeat test oper
= do res <- test
if res
then return ()
else do oper
repeat test oper
Run Code Online (Sandbox Code Playgroud)