首先想象一下,如果你要返回一個列表,你會怎麼做。我認爲這看起來應該很簡單。
groupStrings :: [String] -> [String]
groupStrings [] = []
groupStrings (x:y:z:r) = (x ++ " " ++ y ++ " " ++ z ++ "\n") : groupStrings r
請注意,此模式並非無遺漏:您必須處理列表中有1個或2個元素的情況。這樣做的最簡單的方法是增加更多的情況下:
groupStrings :: [String] -> [String]
groupStrings [] = []
groupStrings [x] = x ++ "\n"
groupStrings [x,y] = x ++ " " ++ y ++ "\n"
groupStrings (x:y:z:r) = (x ++ " " ++ y ++ " " ++ z ++ "\n") : groupStrings r
那麼你的功能是
toFile :: String -> [String] -> IO()
toFile s xs = mapM_ (appendFile s) (groupStrings xs)
如果你願意,你可以內聯的mapM_
和groupStrings
定義,看看是怎麼回事:
toFile :: String -> [String] -> IO()
toFile s [] = return() -- appendFile s "" does nothing
toFile s [x] = appendFile s $ x ++ "\n"
toFile s [x,y] = appendFile s $ x ++ " " ++ y ++ "\n"
toFile s (x:y:z:r) = do
appendFile s (x ++ " " ++ y ++ " " ++ z ++ "\n")
toFile s $ groupStrings r
你也可以寫很好地作爲一個班輪:
import Data.List (intercalate)
import Data.List.Split (chunksOf)
toFile s = mapM_ (\x -> appendFile s $ intercalate " " x ++ "\n") . chunksOf 3