这些"简单"的线条出了点问题......
action = do
isdir <- doesDirectoryExist path -- check if directory exists.
if(not isdir)
then do handleWrong
doOtherActions -- compiling ERROR here.
Run Code Online (Sandbox Code Playgroud)
GHCi会投诉标识符,或者在我添加后不执行最后一行操作else do
.
我认为异常处理可能有效,但是在这种常见的"检查并做某事"声明中是否有必要?
谢谢.
dav*_*420 31
if
在Haskell必须始终有一个then
和一个else
.所以这将有效:
action = do
isdir <- doesDirectoryExist path
if not isdir
then handleWrong
else return ()
doOtherActions
Run Code Online (Sandbox Code Playgroud)
同样,您可以使用when
Control.Monad:
action = do
isdir <- doesDirectoryExist path
when (not isdir) handleWrong
doOtherActions
Run Code Online (Sandbox Code Playgroud)
Control.Monad还有unless
:
action = do
isdir <- doesDirectoryExist path
unless isdir handleWrong
doOtherActions
Run Code Online (Sandbox Code Playgroud)
请注意,当你尝试
action = do
isdir <- doesDirectoryExist path
if(not isdir)
then do handleWrong
else do
doOtherActions
Run Code Online (Sandbox Code Playgroud)
它被解析为
action = do
isdir <- doesDirectoryExist path
if(not isdir)
then do handleWrong
else do doOtherActions
Run Code Online (Sandbox Code Playgroud)