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
2011/12/29 Manfred Lotz <manfred.lotz@arcor.de>
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
Hi Manfred, have a look here: http://hackage.haskell.org/packages/archive/monad-loops/latest/doc/html/Cont... Regards Tim
On Thu, Dec 29, 2011 at 8:29 AM, Tim Baumgartner <baumgartner.tim@googlemail.com> wrote:
2011/12/29 Manfred Lotz <manfred.lotz@arcor.de>
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
....
myall p = mand . and' p
Hi Manfred,
have a look here: http://hackage.haskell.org/packages/archive/monad-loops/latest/doc/html/Cont...
Note that your functions and those of Control.Monad.Loops don't do the same thing : yours don't short circuit, they have to apply the predicate to all the list elements, C.M.L gives you short-circuiting combinators, that answer as soon as possible. You probably want C.M.L behaviour for performance. (I'm lying a bit here, in fact in certain monads (lazy ones), yours could be short-circuiting too, but in IO for instance, that is not the case) -- Jedaï
On Thu, 29 Dec 2011 08:29:41 +0100 Tim Baumgartner <baumgartner.tim@googlemail.com> wrote:
Hi Manfred,
have a look here: http://hackage.haskell.org/packages/archive/monad-loops/latest/doc/html/Cont...
Thanks Tim, didn't know that. -- Manfred
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@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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Markus Läll
On Thu, 29 Dec 2011 12:14:30 +0200 Markus Läll <markus.l2ll@gmail.com> wrote:
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@arcor.de>
This is indeed much easier and clearer. -- Thanks, Manfred
On Thu, Dec 29, 2011 at 06:45:27AM +0100, Manfred Lotz wrote:
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
Note that or' = and' = map. -Brent
On Thu, 29 Dec 2011 11:03:38 -0500 Brent Yorgey <byorgey@seas.upenn.edu> wrote:
On Thu, Dec 29, 2011 at 06:45:27AM +0100, Manfred Lotz wrote:
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
Note that or' = and' = map.
-Brent
Thanks, Brent for pointing me to this. I guess I got it: With or' = map I get myany :: Monad m => (a -> m Bool) -> [a] -> m Bool myany p = mor . map p and with my former definition of mor I get: myany p = liftM or . sequence . map p and then: myany p = liftM or . mapM p which is Markus solution. -- Manfred
participants (5)
-
Brent Yorgey -
Chaddaï Fouché -
Manfred Lotz -
Markus Läll -
Tim Baumgartner