Reputation: 447
I'm new to Haskell.
My objective is to copy files from one directory to other directory.
So far what I have:
I have two lists contain full path file names
list1 = ["file1", "file2" ...]
list2 = ["new name1", "new name2"...]
I want to use
copyFile::FilePath->FilePath->IO()
to copy files from list1 to list2
Note: list2 contains all new full path file names
I know
zipWith(a->b->c)->[a]->[b]->[c]
and I try to
zipWith(copyFile) list1 list2
but it doesn't work.
Any suggestion would be appreciated
Upvotes: 2
Views: 99
Reputation: 440
Since
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
and
System.Directory.copyFile :: FilePath -> FilePath -> IO ()
if you zip
with copyFile
, you get:
zipWith copyFile :: [FilePath] -> [FilePath] -> [IO ()]
that is, given two lists of file paths, you get a list of actions in which each action copies a file. You can evaluate such list of actions using sequence_
:
sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
(in this case, sequence_ :: [IO ()] -> IO ()
).
Thus, something like
sequence_ (zipWith copyFile ["foo", "bar"] ["new_foo", "new_bar"])
would work for you.
EDIT: Even better, as suggested by Daniel Wagner, use Control.Monad.zipWithM_
(zipWithM_ copyFile [...] [...]
).
Upvotes: 6
Reputation: 9767
copyFile
is a monadic action so you cannot use zipWith
on it, here is what you can do:
mapM_ (uncurry copyFile) $ zip list1 list2
note mapM_
's type signature:
λ> :t mapM_
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
Upvotes: 0