[Haskell-beginners] question about any

Markus Läll markus.l2ll at gmail.com
Thu Dec 29 11:14:30 CET 2011


You can use 'mapM' similar to 'map', to get the resultant list in the
monad, and then liftM the function 'or' into it. This way you don't
need to recurse explicitly, like in or' and and'.

many :: Monad m => (a -> m Bool) -> [a] -> m Bool
many p list = or `liftM` mapM p list

(Type of mapM is: Monad m => (a -> m b) -> [a] -> m [b])

On Thu, Dec 29, 2011 at 7:45 AM, Manfred Lotz <manfred.lotz at arcor.de> wrote:
> Hi there,
> might be trivial but anyway.
>
> I have a usage of 'any' and 'all' but I only have a predicate
> p :: a -> IO Bool.
>
> I wrote my own functons for this:
>
>
> mor :: Monad m => [m Bool] -> m Bool
> mor = liftM or . sequence
>
> mand :: Monad m => [m Bool] -> m Bool
> mand = liftM and . sequence
>
> or' :: Monad m => ( a -> m Bool) -> [a] -> [m Bool]
> or' _ [] = []
> or' p (x:xs) = p x : or' p xs
>
> and' :: Monad m => ( a -> m Bool) -> [a] -> [m Bool]
> and' _ [] = []
> and' p (x:xs) = p x : and' p xs
>
> myany :: Monad m => (a -> m Bool) -> [a] -> m Bool
> myany p = mor . or' p
>
> myall :: Monad m => (a -> m Bool) -> [a] -> m Bool
> myall p = mand . and' p
>
>
> which seems to do what I want.
>
>
> Question: Is there any libray function I could use to do this?
>
>
> --
> Thanks,
> Manfred
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



-- 
Markus Läll



More information about the Beginners mailing list