Proposal: Add strictly filtering filterM' to Control.Monad

David Feuer david.feuer at gmail.com
Thu Sep 4 02:05:16 UTC 2014


I wrote filterM' rather wrong. Sorry, folks. Here's a fix:

> filterM' :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
> filterM' p = go []
>   where
>     go acc [] = return (reverse acc)
>     go acc (x:xs) =
>       do
>         flq <- p x
>         if flq then go (x:acc) xs else go acc xs

Or with foldr:

> filterM' :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
> filterM' p xs = foldr go (return . reverse) xs []
>   where
>     go x r = \acc -> do
>              flq <- p x
>              if flq then r (x:acc) else r acc


More information about the Libraries mailing list