Why no findM ? simple Cat revisited

Ahn Ki-yung kyagrd@bawi.org
Wed, 20 Nov 2002 23:33:46 +0900


Jorge Adriano wrote:

>Seems to me like the name findM could be misleading
>mapM :: (Monad m) => (a -> m b) -> [a] -> m [b]
>filterM :: (Monad m) => (a -> m Bool) -> [a] -> m [a]
>
>These take a monadic function and a list of elements. Yours works the other 
>way around (takes a function and a list of 'monadic elements').
>I'd expect the definition of findM to be:
>
>findM'          :: (Monad m) => (a -> m Bool) -> [a] -> m (Maybe a)
>findM' f []     = return Nothing
>findM' f (x:xs) = do { b <- f x; if b then return (Just x) else findM' f xs }
>
>This one doesn't serve your purpose though.
>J.A.
>  
>

I appreciate your comment.
I agree that the type of findM should be the one you suggested,
and it still fits my original purpose. It's no more than a step arout.

\begin{code}

import IO
findM f [] = return Nothing
findM f (x:xs) = do { b <- f x; if b then return (Just x) else findM f xs }

isLeft (Left _) = True
isLeft _ = False

main = findM (>>=return.isLeft) (hCat stdin)
where hCat h = try (hGetLine h>>=putStrLn) : hCat h

\end{code}

I expetct the next Haskell Library Report includes findM.
It's obviously useful.