Lets start with types. What you have is [[String]]
and what you want is [[IO ()]]
i.e each String
gets converted to a IO ()
action (to print it). Lets try to achieve this:
strToPrint :: [[String]] -> [[IO ()]]
strToPrint strs = map (map putStrLn) strs
Now we want [[IO ()]]
to flatten to get [IO ()]
where we insert the desired line break print action as well while flattening the list.
flattenActions :: [[IO ()]] -> [IO ()]
flattenActions actions = concat $ intersperse [(putStrLn "")] actions
Now we can execute these [IO ()]
action one by one using sequence_
. It seems we can combine all these functions into one function using functional composition and hence we get
printStrings :: [[String]] -> IO ()
printStrings = sequence_ . flattenActions . strToPrint
And use it like:
main = printStrings [["a","b","c"],["d","e","f"]]
You can then look for making this more generic and refactor it accordingly.