Skip to content

Instantly share code, notes, and snippets.

@cbzehner
Last active January 14, 2018 01:23
Show Gist options
  • Save cbzehner/c3a2339d52defa89608b931161169221 to your computer and use it in GitHub Desktop.
Save cbzehner/c3a2339d52defa89608b931161169221 to your computer and use it in GitHub Desktop.
Trying to create a set of functions that will get all the files in directory and all of it's subdirectories and so on and so forth
module Files
( getFilesRecursively
) where
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory)
import System.Exit
import System.FilePath (combine)
data PathType = Directory | File | Invalid
deriving (Eq, Ord, Show, Enum)
getFilesRecursively :: FilePath -> IO [FilePath]
getFilesRecursively path = do
pathType <- getPathType path
case pathType of
Invalid -> exitFailure -- TODO: Better error message on invalid paths
File -> pure [path]
Directory -> pure [path, path] -- TODO: Write a recursive function to get directory contents
getFilesFromDirectory :: FilePath -> IO [FilePath]
getFilesFromDirectory path = map (combine path) <$> listDirectory path
getFileTypeTuple :: FilePath -> IO (FilePath, PathType)
getFileTypeTuple path =
fmap (map (\p -> do
pathType <- getPathType
(p, pathType)))
(getFilesFromDirectory path)
getPathType :: FilePath -> IO PathType
getPathType path = do
directoryExists <- doesDirectoryExist path
fileExists <- doesFileExist path
if directoryExists
then pure Directory
else if fileExists
then pure File
else pure Invalid
@cbzehner
Copy link
Author

Error Message

*Main Files Lib> :l Files
[1 of 1] Compiling Files            ( /Users/cbzehner/projects/word-frequency/src/Files.hs, interpreted )
Ok, one module loaded.
*Files> path = "/Users/cbzehner/projects/word-frequency"
*Files> :t getFilesFromDirectory path
getFilesFromDirectory path :: IO [FilePath]
*Files> getFilesFromDirectory path
["/Users/cbzehner/projects/word-frequency/stack.yaml","/Users/cbzehner/projects/word-frequency/Setup.hs","/Users/cbzehner/projects/word-frequency/app","/Users/cbzehner/projects/word-frequency/LICENSE","/Users/cbzehner/projects/word-frequency/test","/Users/cbzehner/projects/word-frequency/ChangeLog.md","/Users/cbzehner/projects/word-frequency/dist","/Users/cbzehner/projects/word-frequency/word-frequency.cabal","/Users/cbzehner/projects/word-frequency/README.md","/Users/cbzehner/projects/word-frequency/.gitignore","/Users/cbzehner/projects/word-frequency/package.yaml","/Users/cbzehner/projects/word-frequency/.git","/Users/cbzehner/projects/word-frequency/.stack-work","/Users/cbzehner/projects/word-frequency/src"]
*Files> :r
[1 of 1] Compiling Files            ( /Users/cbzehner/projects/word-frequency/src/Files.hs, interpreted )

/Users/cbzehner/projects/word-frequency/src/Files.hs:25:3: error:
    • Couldn't match type ‘[(FilePath, b0)]’
                     with ‘(FilePath, PathType)’
      Expected type: IO (FilePath, PathType)
        Actual type: IO [(FilePath, b0)]
    • In the expression:
        fmap
          (map
             (\ p
                -> do pathType <- getPathType
                      (p, pathType)))
          (getFilesFromDirectory path)
      In an equation for ‘getFileTypeTuple’:
          getFileTypeTuple path
            = fmap
                (map
                   (\ p
                      -> do pathType <- getPathType
                            (p, pathType)))
                (getFilesFromDirectory path)
   |
25 |   fmap (map (\p -> do
   |   ^^^^^^^^^^^^^^^^^^^...

/Users/cbzehner/projects/word-frequency/src/Files.hs:26:17: error:
    • Couldn't match expected type ‘(FilePath, b0)’
                  with actual type ‘FilePath -> IO PathType’
    • Probable cause: ‘getPathType’ is applied to too few arguments
      In a stmt of a 'do' block: pathType <- getPathType
      In the expression:
        do pathType <- getPathType
           (p, pathType)
      In the first argument of ‘map’, namely
        ‘(\ p
            -> do pathType <- getPathType
                  (p, pathType))’
   |
26 |     pathType <- getPathType
   |                 ^^^^^^^^^^^
Failed, no modules loaded.
Prelude>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment