[Haskell-beginners] Help! Trapped in the IO Monad!

Alexander Dunlap alexander.dunlap at gmail.com
Tue Jan 27 23:14:27 EST 2009


You can do (something like; this is untested)

splitDirFile :: [FilePath] -> IO ([FilePath],[FilePath])
splitDirFile [] = return ([],[])
splitDirFile (f:fs) = do
  (yess,nos) <- splitDirFile fs
  exists <- doesDirectoryExist f
  return $ if exists
    then (f:yess,nos)
    else (yess,f:nos)

You might also look at Control.Monad.filterM. I often define a
function "partitionM" which is like partition except it uses a monadic
test, just like you have.

Alex

On Tue, Jan 27, 2009 at 8:07 PM, Erik de Castro Lopo
<mle+cl at mega-nerd.com> wrote:
> Hi all,
>
> I have a list of entries for a directory (FilePaths) and I'd like to
> partition them into files and directories using Data.List.partition:
>
>    partition :: [a] -> ([a], [a])
>
> Now, one solution is to use unsafePerformIO:
>
>    splitDirFile :: [FilePath] -> ([FilePath], [FilePath])
>    splitDirFile paths = do
>        partition (\p -> unsafePerformIO (doesDirectoryExist p)) paths
>
> Two questions:
>
>  a) Is it possible to do this without invoking unsafePerformIO? Ie with
>     a function signature of say:
>
>         partition :: [FilePath] -> IO ([FilePath], [FilePath])
>
>  b) Exactly how unsafe is the unsafePerformIO version?
>
> Erik
> --
> -----------------------------------------------------------------
> Erik de Castro Lopo
> -----------------------------------------------------------------
> The main confusion about C++ is that its practitioners think
> it is simultaneously a  high and low level language when in
> reality it is good at neither.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list