简体   繁体   English

使用Haskell读取行并反转文本

[英]Reading lines and reversing the text with Haskell

My program should do the following: 我的程序应执行以下操作:

  • Read lines from a file 读取文件中的行
  • For each line, reverse the text of each word ("bat cat hat" becomes "tab tac tah") 对于每一行,请反转每个单词的文本(“ bat cat hat”变成“ tab tac tah”)
  • Print out the the reverse text 打印出相反的文字

But it doesn't work and being a Haskell-newbie, I can't understand the errors. 但这是行不通的,作为Haskell新手,我无法理解错误。

Here is my code: 这是我的代码:

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

And my errors: 我的错误:

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

Firstly, your last line in your main function is of type [String] -> IO (), and you need it to be IO (), so you actually need to pass it the list of strings. 首先,您的main函数中的最后一行是[String]-> IO()类型,并且您需要将其作为IO(),因此实际上需要向其传递字符串列表。 Here is one way to do what you are requesting: 这是您要执行的操作的一种方法:

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)

And here is a recursive version akin to what you wanted to do in your version 这是类似于您要在版本中执行的递归版本

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

I've also fixed your version with minimal changes, but bear in mind that this is not really the Haskell way to do things. 我还通过最少的更改修复了您的版本,但请记住,这实际上并不是Haskell的处理方式。 Go with one of the options I provided above. 使用我上面提供的选项之一。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM