简体   繁体   中英

Writing to file recursive implementation in Haskell

I'm trying to create a method that, given the name of the file where to write to and a list of strings, writes to file the contents of that list, 3 strings at a time.

eg

James Philipp Francis
Carl Tom Matt
Audrey Adam Patrick

So far, I have this:

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

But I don't know how to apply recursivity in IO... Any help would be appreciated.

Thanks in advance.

First imagine what you would do if you were to return a list. I think it should seem pretty straightforward.

groupStrings :: [String] -> [String]
groupStrings [] = [] 
groupStrings (x:y:z:r) = (x ++ " " ++ y ++ " " ++ z ++ "\n") : groupStrings r

Note that this pattern is non-exhaustive: you have to handle the case where the list has 1 or 2 elements. The simplest way of doing this is adding more cases:

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

Then your function is

toFile :: String -> [String] -> IO ()
toFile s xs = mapM_ (appendFile s) (groupStrings xs)

If you want, you can inline the definitions of mapM_ and groupStrings to see what is going on:

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

You can also write this quite nicely as a one-liner:

import Data.List (intercalate)
import Data.List.Split (chunksOf)
toFile s = mapM_ (\x -> appendFile s $ intercalate " " x ++ "\n") . chunksOf 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM