我的程序应该执行以下操作:
- 从文件中读取行
- 对于每一行,反转每个单词的文本(“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