2014-12-13 72 views
2

我想創建一個方法,給定要寫入的文件的名稱和字符串列表,寫入文件列表中的內容,一次3個字符串。在Haskell中寫入文件遞歸實現

例如

James Philipp Francis 
Carl Tom Matt 
Audrey Adam Patrick 

到目前爲止,我有這樣的:

toFile :: String -> [String] -> IO() 
toFile s [] = appendFile s "" 
toFile s (x:y:z:xs) = appendFile s (x ++ " " ++ y ++ " " ++ z ++ "\n") 

但我不知道如何在IO應用遞歸性...任何幫助,將不勝感激。

在此先感謝。

回答

4

首先想象一下,如果你要返回一個列表,你會怎麼做。我認爲這看起來應該很簡單。

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