Proposal: Add sequenceWhile and SequenceWhile_ to Control.Monad

Sittampalam, Ganesh ganesh.sittampalam at credit-suisse.com
Sat May 9 10:25:05 EDT 2009


Dominic Steinitz wrote:
> I'm not sure of the protocol for adding things to existing libraries

http://www.haskell.org/haskellwiki/Library_submissions

Though my general preference is to kick around an idea on here for a
while first before starting the formal process.

> but I'd like to propose the addition of these two functions to
> Control.Monad. 
> 
> sequenceWhile_ :: Monad m => (a -> Bool) -> [m a] -> m ()
> sequenceWhile_ _ [] =
>   return ()
> sequenceWhile_ p (x:xs) =
>   x >>= \c -> if (p c) then sequenceWhile_ p xs else return ()
> 
> sequenceWhile :: Monad m => (a -> Bool) -> [m a] -> m [a]
> sequenceWhile _ [] =
>   return []
> sequenceWhile p (x:xs) = do
>   y <- x
>   if (p y)
>     then do
>       ys <- sequenceWhile p xs
>       return (y:ys)
>     else
>       return [y]

This last line should probably be return [] for consistency with
takeWhile etc. The downside of that is that the monadic effect
associating with producing y has been run but then the value is thrown
away.

It's also a bit specialised for my taste. A better option would be to
have a takeWhileM which would allow sequenceWhile_ and sequenceWhile to
be built from that and sequence_/sequence.

Cheers,

Ganesh

=============================================================================== 
 Please access the attached hyperlink for an important electronic communications disclaimer: 
 http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html 
 =============================================================================== 
 


More information about the Libraries mailing list