[Haskell-cafe] version of findIndex that works with a monadic predicate

José Romildo Malaquias j.romildo at gmail.com
Fri Nov 26 20:46:41 CET 2010


I need a function findIndexM, similar to findIndex from the standard
module Data.List, but which works with a monadic predicate to test list

findIndex :: (a -> Bool) -> [a] -> Maybe Int

findIndexM :: (Monad m, Num a) => (t -> m Bool) -> [t] -> m (Maybe a)

findIndexM p xs = go 0 xs
    go _ [] = return Nothing
    go n (x:xs) = do res <- p x
                     if res then return (Just n) else go (n+1) xs

How can this function be rewritten using combinators?


More information about the Haskell-Cafe mailing list