14

重複の可能性:
HaskellはIOを「何もしない」、または他にない場合

これらの「簡単な」行で問題が発生しました...

action = do
    isdir <- doesDirectoryExist path  -- check if directory exists.
    if(not isdir)                     
        then do handleWrong
    doOtherActions                    -- compiling ERROR here.

GHCiは識別子について苦情を申し立てるか、追加した後の最後の行のアクションを実行しませんelse do

例外処理は機能するかもしれないと思いますが、そのような一般的な「チェックして何かをする」ステートメントでは必要ですか?

ありがとう。

4

1 に答える 1

33

ifHaskellでは常にとが必要thenですelse。したがって、これは機能します:

action = do
    isdir <- doesDirectoryExist path
    if not isdir
        then handleWrong
        else return ()     -- i.e. do nothing
    doOtherActions

when同様に、 Control.Monadから使用できます。

action = do
    isdir <- doesDirectoryExist path
    when (not isdir) handleWrong
    doOtherActions

Control.Monadには次のものもありますunless

action = do
    isdir <- doesDirectoryExist path
    unless isdir handleWrong
    doOtherActions

あなたが試したときに注意してください

action = do
    isdir <- doesDirectoryExist path
    if(not isdir)
        then do handleWrong
        else do
    doOtherActions

次のように解析されました

action = do
    isdir <- doesDirectoryExist path
    if(not isdir)
        then do handleWrong
        else do doOtherActions
于 2011-05-07T12:30:07.287 に答える