I want a sorted list of files from a directory. How do I apply the sort function to a list with IO monad?
import System.Directory
import Data.List
sortedFiles :: FilePath -> IO [FilePath]
sortedFiles path = do
files <- getDirectoryContents "."
return sort files -- this does not work
The original problem is just lack of parentheses, as currently
returnis being applied to two arguments (sortandfiles), just fix that up:If you want you can
fmapthe sort function over the directory contents. It kind of has a nice, direct feel to it, basically lifting the sort function up into the IO monad: