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

Heinrich Apfelmus apfelmus at quantentunnel.de
Thu Jan 29 05:58:51 EST 2009


Alexander Dunlap wrote:
> 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.

How about

  splitDirFile ps =
    ((map fst *** map fst) . partition snd . zip ps)
    `liftM` mapM doesDirectoryExist ps

There is no need to rewrite  partition , you can reuse it.


Hm, the plumbing seems slightly cumbersome to me, maybe Conal's editor
combinators

  http://conal.net/blog/posts/semantic-editor-combinators/

can help.


Regards,
apfelmus

-- 
http://apfelmus.nfshost.com



More information about the Beginners mailing list