Why no findM ? simple Cat revisited
Simple Cat (revisitied) \begin{code} import IO findM f [] = return Nothing findM f (x:xs) = do { v <- x; if f v then return (Just v) else findM f xs } isLeft (Left _) = True isLeft _ = False main = findM (isLeft) (hCat stdin) where hCat h = try (hGetLine h) : hCat h \end{code} This is my answer for the question of my own, which is posted a couple There are mapM, filterM in the Haskell 98 Standard Library. But why no findM there ? As you can see from simple cat, it seems quite useful. I think fildM should be added to the module Monad. -- Ahn Ki-yung
Simple Cat (revisitied)
\begin{code}
import IO
findM f [] = return Nothing findM f (x:xs) = do { v <- x; if f v then return (Just v) else findM f xs }
isLeft (Left _) = True isLeft _ = False
main = findM (isLeft) (hCat stdin) where hCat h = try (hGetLine h) : hCat h
\end{code}
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.
participants (2)
-
Ahn Ki-yung -
Jorge Adriano