3

次のコードブロックはなぜですか?

main = do
    line <- getLine
    if null line
        then runTestTT tests
        else do
            line2 <- getLine
            seq::[Int] <- return $ map read $ words line2
            print $ process seq

エラーをスローします:

lgis.hs:28:13:
    Couldn't match type `()' with `Counts'
    Expected type: IO Counts
      Actual type: IO ()
    In a stmt of a 'do' block: print $ process seq
    In the expression:
      do { line2 <- getLine;
           seq :: [Int] <- return $ map read $ words line2;
           print $ process seq }
    In a stmt of a 'do' block:
      if null line then
          runTestTT tests
      else
          do { line2 <- getLine;
               seq :: [Int] <- return $ map read $ words line2;
               print $ process seq }

両方にもかかわらず:

main = do
    runTestTT tests

main = do
    line <- getLine
    line2 <- getLine
    seq::[Int] <- return $ map read $ words line2
    print $ process seq

うまくいきますか?

4

1 に答える 1

9

の両方のブランチはif then else同じタイプである必要がありますが、

runTestTT tests :: IO Counts

print $ process seq :: IO ()

ブランチにを追加できます。これを行うための最新の方法は、を使用するreturn ()ことです。thenControl.Monad.void

main = do
    line <- getLine
    if null line
        then void (runTestTT tests)         -- formerly: runTestTT tests >> return ()
        else do
            line2 <- getLine
            seq::[Int] <- return $ map read $ words line2
            print $ process seq

それを修正します(またはブランチreturn some_value_of_type_Countsにを追加することもできます)。else

于 2013-03-16T13:25:37.237 に答える