1
我在考試前試圖學習Haskell,它對我來說仍然很神奇。今天,我試圖編寫一個程序,它讀取一個文件,並將它的行按照順序寫入另一個文件。結果? 。錯誤:(大量的這裏是我的代碼:文件中的反轉行
import IO
readLines :: Handle -> [String] -> [String]
readLines handler list = do
eof <- hIsEOF handler
if eof then list
else do
line <- hGetLine handler
readLines handler (list ++ [line])
writeLines :: Handle -> [String] -> [String]
writeLines handler list = if length list == 0 then list
else do
line <- head list
hPutStrLn handler line
writeLines tail list
fileToList :: FilePath -> [String]
fileToList filename = do
handler <- openFile filename ReadMode
list <- (readLines handler [])
hClose handler
list
invLines :: FilePath -> FilePath -> IO()
invLines input output = do
handler <- openFile output WriteMode
inverted <- reverse (fileToList input)
writeLines handler inverted
hClose handler
main = invLines "in.txt" "out.txt"
我會很感激,如果你可以給我解釋一下我的錯誤