1

私のプログラムは次のことを行う必要があります。

  • ファイルから行を読み取る
  • 行ごとに、各単語のテキストを逆にします (「bat cat hat」は「tab tac tah」になります)。
  • 反転テキストを印刷する

しかし、それは機能せず、Haskell初心者であるため、エラーを理解できません。

これが私のコードです:

main = do
    content <- readFile "test.txt"
    let linesList = lines content
    reverseLines

reverseWords :: String -> String
reverseWords = unwords . map reverse . words

reverseLines :: [String] -> IO ()
reverseLines inputList = do
    if null inputList
        then return ()
        else do
        line <- inputList!!0
        if null line
            then return ()
            else do
                putStrLn $ reverseWords line
                reverseLines . tail inputList

そして私のエラー:

readlines.hs:4:9:
    Couldn't match expected type `IO b0'
                with actual type `[String] -> IO ()'
    In a stmt of a 'do' block: reverseLines
    In the expression:
      do { content <- readFile "test.txt";
           let linesList = lines content;
           reverseLines }
    In an equation for `main':
        main
          = do { content <- readFile "test.txt";
                 let linesList = ...;
                 reverseLines }

readlines.hs:14:25:
    Couldn't match type `[Char]' with `IO [Char]'
    Expected type: [IO [Char]]
      Actual type: [String]
    In the first argument of `(!!)', namely `inputList'
    In a stmt of a 'do' block: line <- inputList !! 0
    In the expression:
      do { line <- inputList !! 0;
           if null line then
               return ()
           else
               do { putStrLn $ reverseWords line;
                    .... } }

readlines.hs:19:33:
    Couldn't match expected type `IO ()' with actual type `a0 -> IO ()'
    In a stmt of a 'do' block: reverseLines . tail inputList
    In the expression:
      do { putStrLn $ reverseWords line;
           reverseLines . tail inputList }
    In a stmt of a 'do' block:
      if null line then
          return ()
      else
          do { putStrLn $ reverseWords line;
               reverseLines . tail inputList }

readlines.hs:19:48:
    Couldn't match expected type `a0 -> [String]'
                with actual type `[String]'
    In the return type of a call of `tail'
    Probable cause: `tail' is applied to too many arguments
    In the second argument of `(.)', namely `tail inputList'
    In a stmt of a 'do' block: reverseLines . tail inputList
4

1 に答える 1

2

まず、main関数の最後の行はタイプ [String] -> IO () であり、IO () である必要があるため、実際には文字列のリストを渡す必要があります。あなたが要求していることを行う1つの方法は次のとおりです。

main :: IO ()
main = do
    content <- readFile "test.txt"
    let linesList = lines content
    reverseLines linesList

reverseWords :: String -> String
reverseWords = unwords . map reverse . words

reverseLines :: [String] -> IO ()
reverseLines = mapM_ (putStrLn . reverseWords)

そして、これがあなたのバージョンでやりたかったことに似た再帰的なバージョンです

reverseLines2 :: [String] -> IO ()
reverseLines2 []      = return ()
reverseLines2 (x:xs)  = do
  putStrLn (reverseWords x)
  reverseLines2 xs

あなたのバージョンも最小限の変更で修正しましたが、これは実際には Haskell のやり方ではないことに注意してください。上記で提供したオプションのいずれかを使用してください。

main = do
    content <- readFile "test.txt"
    let linesList = lines content
    reverseLines linesList

reverseWords :: String -> String
reverseWords = unwords . map reverse . words

reverseLines :: [String] -> IO ()
reverseLines inputList = do
    if null inputList
        then return ()
    else do
      let line = head inputList 
      if null line
          then return ()
          else do
              putStrLn $ reverseWords line
              reverseLines $ tail inputList
于 2013-11-13T14:31:24.413 に答える