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

Maciej Piechotka uzytkownik2 at gmail.com
Sat Nov 27 04:26:08 CET 2010


On Sat, 2010-11-27 at 02:23 +0300, Miguel Mitrofanov wrote:
> findIndexM = (liftM (findIndex id) .) . mapM


Not quite. Compare:

findIndexM (\x -> print x >> return True) [1,2,3]

or

findIndexM (\x -> if x == 2 then Nothing else Just True) [1,2,3]

Possibly better:

findIndexM p = foldr (\(n, y) x ->
                        p y >>= \b -> if b then return $! Just $! n
                                           else x) (return $! Nothing) .
               zip (map fromIntegral [1..]))

Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20101126/6a531937/attachment.bin


More information about the Haskell-Cafe mailing list