UP | HOME

Max File Size

While doing some refactoring work on solution from this guy1, I realized a pretty trivial thing about Manod method >>=.

Take a look at its definition:

  Prelude> :t (>>=)
  (>>=) :: (Monad m) => m a -> (a -> m b) -> m b

Both a and b ought to be some Monadic type! In other words, all computation glued together by >>= have same Manodic type.

It is really straightforward however I did not have that in mind.

Since I did want to define a function take a String and return a list of String while its implementation has several computations that return IO type. Obviously the function failed to compiled. Therefore I changed it to return IO [String].

  -- FilePath is from package System.IO and it is synonyms of String
  getFilesInDir :: FilePath -> IO [FilePath]
  getFilesInDir inp = do
    isDir <- doesDirectoryExist inp
    files <- if isDir then
                (do
                 names <- getDirectoryContents inp
                 forM [ inp </> x | x <- names, isNotSpecialDir x ] getFilesInDir)
             else return [[inp]]
    return $ concat files

PS:

Footnotes: