Why no findM ? simple Cat revisited

Jorge Adriano jadrian@mat.uc.pt
Wed, 20 Nov 2002 13:17:47 +0000


> Simple Cat (revisitied)
>
> \begin{code}
>
> import IO
>
> findM f [] =3D return Nothing
> findM f (x:xs) =3D do { v <- x; if f v then return (Just v) else findM =
f xs }
>
> isLeft (Left _) =3D True
> isLeft _ =3D False
>
> main =3D findM (isLeft) (hCat stdin) where hCat h =3D try (hGetLine h) =
: hCat h
>
> \end{code}

Seems to me like the name findM could be misleading
mapM :: (Monad m) =3D> (a -> m b) -> [a] -> m [b]
filterM :: (Monad m) =3D> (a -> m Bool) -> [a] -> m [a]

These take a monadic function and a list of elements. Yours works the oth=
er=20
way around (takes a function and a list of 'monadic elements').
I'd expect the definition of findM to be:

findM'          :: (Monad m) =3D> (a -> m Bool) -> [a] -> m (Maybe a)
findM' f []     =3D return Nothing
findM' f (x:xs) =3D do { b <- f x; if b then return (Just x) else findM' =
f xs }

This one doesn't serve your purpose though.
J.A.