`mapM_`、`copyFile` 和一个 ghost 文件
`mapM_`, `copyFile`, and a ghost file
我在复制 Haskell 中的文件列表时遇到一个奇怪的问题。如果我运行下面的代码:
copy :: [FilePath] -> FilePath -> IO ()
-- Precondition: dir must be a directory.
copy fs dir = do
isDir <- doesDirectoryExist dir
if (isDir)
then do
mapM_ putStrLn fs -- Poor man's debug.
mapM_ (`copyFile` dir) fs
else ioError (userError $ dir ++ " is not a directory.")
mapM_ putStrLn fs
的输出给出了一个存在的文件,但是第二个 mapM_
失败并显示以下消息:
./.copyFile4363.tmp: copyFile: inappropriate type (Is a directory)
我真的很困惑,因为在 mapM_
的两种用法中,列表 fs
都作为参数传递。
我是不是忽略了什么?
来自 System.Directory Haddock(强调我的):
copyFile :: FilePath -> FilePath -> IO () Source
copyFile old new
copies the existing file from old to new. If the new file already exists, it is atomically replaced by the old file. Neither path may refer to an existing directory. The permissions of old are copied to new, if possible.
我在复制 Haskell 中的文件列表时遇到一个奇怪的问题。如果我运行下面的代码:
copy :: [FilePath] -> FilePath -> IO ()
-- Precondition: dir must be a directory.
copy fs dir = do
isDir <- doesDirectoryExist dir
if (isDir)
then do
mapM_ putStrLn fs -- Poor man's debug.
mapM_ (`copyFile` dir) fs
else ioError (userError $ dir ++ " is not a directory.")
mapM_ putStrLn fs
的输出给出了一个存在的文件,但是第二个 mapM_
失败并显示以下消息:
./.copyFile4363.tmp: copyFile: inappropriate type (Is a directory)
我真的很困惑,因为在 mapM_
的两种用法中,列表 fs
都作为参数传递。
我是不是忽略了什么?
来自 System.Directory Haddock(强调我的):
copyFile :: FilePath -> FilePath -> IO () Source
copyFile old new
copies the existing file from old to new. If the new file already exists, it is atomically replaced by the old file. Neither path may refer to an existing directory. The permissions of old are copied to new, if possible.