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 (),因此您实际上需要将字符串列表传递给它。这是执行您要求的一种方法:

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 回答