Copying files in Haskell I recently watched a video in which the speaker said that Haskell is not suited for real-world tasks, such as copying files from one folder to another.
So I tried to write a program that copies a given file system tree to a new location. The usage of the program is

copy_tree &lt;source path&gt; &lt;target path&gt;


If the target path does not exists, it is created. Then all folders and files under &lt;source path&gt; are copied to &lt;target path&gt;. Here is my copy_tree.hs source. I would like to have feedback and suggestions on how to improve it. Especially, I would like to make it as succinct and as idiomatic as possible.

One thing that I do not like very much is that the code calls doesDirectoryExist twice.

import System.Directory
import System.Environment
import System.FilePath


getSubitems path = getSubitemsRec ""
  where
    getChildren path = do
        isDir &lt;- doesDirectoryExist path
        if isDir
          then fmap (filter (`notElem` [".", ".."])) (getDirectoryContents path)
          else return []

    getSubitemsRec relPath = do
        children &lt;- getChildren (path &lt;/&gt; relPath)
        let relChildren = [relPath &lt;/&gt; p | p &lt;- children]
        nephewItems &lt;- mapM getSubitemsRec relChildren
        return (relChildren ++ (concat nephewItems))


copyItem baseSourcePath baseTargetPath relativePath = do
    let sourcePath = baseSourcePath &lt;/&gt; relativePath
    let targetPath = baseTargetPath &lt;/&gt; relativePath

    putStrLn $ "Copying " ++ sourcePath ++ " to " ++ targetPath
    isDir &lt;- doesDirectoryExist sourcePath
    if isDir
      then createDirectoryIfMissing False targetPath
      else copyFile sourcePath targetPath


copyTree s t = do
    subItems &lt;- getSubitems s
    createDirectoryIfMissing True t
    mapM_ (copyItem s t) subItems


main = do
    args &lt;- getArgs
    if (length args) == 2
      then copyTree (args !! 0) (args !! 1)
      else putStrLn "Usage: copy_tree &lt;source&gt; &lt;target&gt;"
One thing that I do not like very much is that the code calls
  doesDirectoryExist twice.


You can return the result of the doesDirectoryExist from getSubitems and pass it to copyItem, obviating the duplicate call.


  Especially, I would like to make it as succinct and as idiomatic as
  possible.


You can use low precedence &lt;$&gt; instead of fmap to remove some parentheses.

Also "." and ".." should be returned once from getDirectoryContents, so you can replace the

(filter (`notElem` [".", ".."])) 


with (\\ [".", ".."]).

Slight Behavior Difference between copyTree and copyTree'

Behavior of copyTree' is slightly different from copyTree, in that it will accept files as well as directories as parameter s. This is due to the fact that getSubitems' includes the input parameter (path, root of the subtree) in the descendants list it returns. You can get back the previous behavior by ignoring the first value returning from the getSubitems' in copyTree'. But I left the change in copyTree' as is for a behavior more consistent with cp -R of POSIX.

Modified Code

import Data.Functor ((&lt;$&gt;))
import Data.List ((\\))

getSubitems' :: FilePath -&gt; IO [(Bool, FilePath)]
getSubitems' path = getSubitemsRec ""
  where
    getChildren path =  (\\ [".", ".."]) &lt;$&gt; getDirectoryContents path

    getSubitemsRec relPath = do
        let absPath = path &lt;/&gt; relPath
        isDir &lt;- doesDirectoryExist absPath
        children &lt;- if isDir then getChildren absPath else return []
        let relChildren = [relPath &lt;/&gt; p | p &lt;- children]
        ((isDir, relPath) :) . concat &lt;$&gt; mapM getSubitemsRec relChildren

copyItem' baseSourcePath baseTargetPath (isDir, relativePath) = do
    let sourcePath = baseSourcePath &lt;/&gt; relativePath
    let targetPath = baseTargetPath &lt;/&gt; relativePath

    putStrLn $ "Copying " ++ sourcePath ++ " to " ++ targetPath
    if isDir
      then createDirectoryIfMissing False targetPath
      else copyFile sourcePath targetPath

copyTree' s t = do
    createDirectoryIfMissing True t
    subItems &lt;- getSubitems' s
    mapM_ (copyItem' s t) subItems