我在這裏練習http://book.realworldhaskell.org/read/functional-programming.html。我需要轉置文本文件的問題的解決方案似乎需要大量的CPU時間。如果可以的話,我如何改進算法以下的算法,使其更少地佔用CPU。在Haskell中移位文本文件
import System.Environment (getArgs)
import Data.Char(isAlpha)
interactWith function inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (function input)
main = mainWith myFunction
where mainWith function = do
args <- getArgs
case args of
[input,output] -> interactWith function input output
_ -> putStrLn "error: exactly two arguments needed"
-- replace "id" with the name of our function below
myFunction = transpose
transpose :: String -> String
transpose input = tpose (lines input)
tpose [] = []
tpose xs = concat (map (take 1) xs) ++ "\n" ++ tpose (map (drop 1) xs)
這是否使速度更快? – 2012-10-22 11:32:19