diff options
| author | Jan Sucan <jan@jansucan.com> | 2025-08-31 16:25:47 +0200 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2025-08-31 16:25:47 +0200 |
| commit | c4cf3b0be2606b144d5646ef4f5fc295cab1e877 (patch) | |
| tree | beef6c398fcd99e7e4db1af5c9dd2153567e0222 | |
| parent | 9fc6c7f42607ea44d4845fe5990235286405562d (diff) | |
18_a_1: Add missing module CountEntries from the examples
| -rw-r--r-- | ch18/CountEntries.hs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/ch18/CountEntries.hs b/ch18/CountEntries.hs new file mode 100644 index 0000000..706bec2 --- /dev/null +++ b/ch18/CountEntries.hs @@ -0,0 +1,22 @@ +{-- snippet countEntriesTrad --}
+module CountEntries (listDirectory, countEntriesTrad) where
+
+import System.Directory (doesDirectoryExist, getDirectoryContents)
+import System.FilePath ((</>))
+import Control.Monad (forM, liftM)
+
+listDirectory :: FilePath -> IO [String]
+listDirectory = liftM (filter notDots) . getDirectoryContents
+ where notDots p = p /= "." && p /= ".."
+
+countEntriesTrad :: FilePath -> IO [(FilePath, Int)]
+countEntriesTrad path = do
+ contents <- listDirectory path
+ rest <- forM contents $ \name -> do
+ let newName = path </> name
+ isDir <- doesDirectoryExist newName
+ if isDir
+ then countEntriesTrad newName
+ else return []
+ return $ (path, length contents) : concat rest
+{-- /snippet countEntriesTrad --}
|
