这是一个程序,它应该输入未知数量的文本行,并以相反的顺序打印每行两次 样本输入:
LINE 1
LINE 2
EOF
实际输出:
LINE 2
LINE 2
LINE 1
LINE 1
和EOF表示停止接受输入。
程序编译($ ghc filename.hs
)就好了,但在输入时没有发生解析错误($ filename: user error (Prelude.readIO: no parse)
)
import Data.Array
import Data.List
import Data.Ord
main = do {
ls <- readlist [];
putStrLn (show (reverse ls));
}
readlist :: [String] -> IO [String]
readlist l = do {
inp <- readLn :: IO String;
if (inp == "EOF") then
return l;
else readlist (inp:l);
}
printOften :: String -> IO ()
printOften str = do {
putStrLn str;
putStrLn str;
}
答案 0 :(得分:0)
在处理完这段代码后,我发现这段代码需要付出很多努力才能解决,所以我稍微改了一下代码,而不是删除错误。
import Data.Array
import Data.List
import Data.Ord
main = do {
ls <- readlist [];
printOften ls;
}
readlist :: [String] -> IO [String]
readlist xs = do{
imp <- getLine :: IO String;
if (imp=="EOF")
then return xs
else readlist (imp:xs)
}
printOften :: [String] -> IO ()
printOften [] = return ()
printOften (x:xs) = do {
putStrLn x;
putStrLn x;
printOften xs;
}