Created
December 5, 2012 09:51
-
-
Save davidxifeng/4214375 to your computer and use it in GitHub Desktop.
foldM and foldr &foldl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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