Skip to content

Instantly share code, notes, and snippets.

@davidxifeng
Created December 5, 2012 09:51
Show Gist options
  • Save davidxifeng/4214375 to your computer and use it in GitHub Desktop.
Save davidxifeng/4214375 to your computer and use it in GitHub Desktop.
foldM and foldr &foldl
import System.Directory
import Control.Monad
selectM :: Monad m => (a -> m Bool) -> ([a], [a]) -> a -> m ([a], [a])
selectM p ~(ts,fs) x = do
r <- p x
if r then return (x:ts, fs) else return (ts, x:fs)
partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a],[a])
partitionM p xs = do
-- selectM p x ([], [])
-- foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
foldM (selectM p ) ([],[]) xs
test = do
partitionM doesDirectoryExist ["/home/david","/","/home","/tmp/test/t.hs"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment