3

Haskellでファイルを読み込もうとしていますが、例外をキャッチしていますが、動作しません。コードは次のようになります。

     module Main where
    import System.Environment
    import System.IO
    import System.Exit

    main = do
        x:xs <- getArgs
        case length(x:xs) of
            2 -> do catch (readFile x)
                        (\_ -> do   putStrLn ("Error on reading file: " ++ x) 
                                    getLine
                                    exitWith ExitSuccess)
            _ -> do putStrLn ("Run this way: ./projekt inputFile RE") >>
                exitFailure

そして、私はこのエラーを受け取ります:

Couldn't match expected type `IO String
                              -> (ExitCode -> IO a)
                              -> ExitCode
                              -> IO String'
       against inferred type `IO ()'
In the expression:
    putStrLn
      ("Error on reading file: " ++ x) getLine exitWith ExitSuccess
In the expression:
    do { putStrLn
           ("Error on reading file: " ++ x) getLine exitWith ExitSuccess }
In the second argument of `catch', namely
    `(\ _ -> do { putStrLn
                    ("Error on reading file: " ++ x) getLine exitWith ExitSuccess })'

ヒントを教えていただけますか?ありがとう

4

2 に答える 2

3

13行目に余分な(または余分な)があり>>ます:do

_ -> do putStrLn ("Run this way: ./projekt inputFile RE") >>

する必要があります:

_ -> do putStrLn ("Run this way: ./projekt inputFile RE")

また:

_ -> putStrLn ("Run this way: ./projekt inputFile RE") >> exitFailure

完全なコード:

main = do
l@(x:xs) <- getArgs
case length l of
    2 -> do catch (readFile x) $ \_ -> do
            putStrLn $ "Error on reading file: " ++ x
            getLine
            exitWith ExitSuccess
    _ -> do putStrLn $ "Run this way: ./projekt inputFile RE"
            exitFailure
于 2011-03-07T21:52:09.077 に答える
0

インデントを確認してください。

貼り付けたコードは問題ないように見えますが、エラーメッセージは、getLineとが上記exitWith ExitSuccessよりもさらにインデントされている可能性があることを示していputStrLnます。おそらくこれはスペース-v-タブの問題ですか?

于 2011-03-07T22:34:37.730 に答える