I'm trying to compile this function but I get an error. What is it that Haskell is not happy about?
copyFile :: FilePath -> FilePath -> IO()
copyFile fromFile toFile =
do c <- readFile fromFile
writeFile toFile c
Unexpected do block in function application: do c <- readFile fromFile You could write it with parentheses Or perhaps you meant to enable BlockArguments?
The indentation is not entirely correct, and as a result the compiler parses this as if the do
is only applied to the c <- readFile fromFile
, and not to the writeFile
.
copyFile :: FilePath -> FilePath -> IO()
copyFile fromFile toFile = do
c <- readFile fromFile
writeFile toFile c
or shorter:
copyFile :: FilePath -> FilePath -> IO()
copyFile fromFile toFile = readFile fromFile >>= writeFile toFile
That being said, I strongly recommend to use copyFile :: FilePath -> FilePath -> IO ()
or copyFileWithMetadata :: FilePath -> FilePath -> IO ()
instead. That way you do not load the content of the file in memory (the file might be larger than the amount of available memory). Some file systems could improve the efficiency significantly, for example by only adding an extra reference to the file, and making a real copy in case one of the two files is modified.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments