martin
martin

Reputation: 1182

Write to multiple files in Haskell

In Haskell, how would one go about writing to an arbitrarily large number of files?

As an example, let's say I want to take the letters a through z and put them in files named for the letter of their contents. An initial attempt was to do the following:

main :: IO ()
main = do
       let letter = map (:"") ['a'..'z']
       zipWith writeFile letter letter

which produced the following error:

Couldn't match expected type 'IO ()' with actual type '[IO ()]'

I feel like there should be a way to loop through a list in a do block, but I haven't been able to find it yet.

Upvotes: 1

Views: 398

Answers (1)

arrowd
arrowd

Reputation: 34401

The function you need is zipWithM.

Re. way to loop through a list in a do block, there is also a function for this - sequence, so you can write sequence $ zipWith writeFile letter letter.

Upvotes: 2

Related Questions