Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  question about any (Manfred Lotz)
   2. Re:  question about any (Tim Baumgartner)
   3. Re:  The numeric type stack (Brandon Allbery)
   4. Re:  question about any (Chadda? Fouch?)
   5. Re:  question about any (Markus L?ll)


----------------------------------------------------------------------

Message: 1
Date: Thu, 29 Dec 2011 06:45:27 +0100
From: Manfred Lotz <manfred.l...@arcor.de>
Subject: [Haskell-beginners] question about any
To: beginners@haskell.org
Message-ID: <20111229064527.1f94c...@arcor.com>
Content-Type: text/plain; charset=US-ASCII

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




------------------------------

Message: 2
Date: Thu, 29 Dec 2011 08:29:41 +0100
From: Tim Baumgartner <baumgartner....@googlemail.com>
Subject: Re: [Haskell-beginners] question about any
To: Manfred Lotz <manfred.l...@arcor.de>
Cc: beginners@haskell.org
Message-ID:
        <cae0z8dgf9n8gualzshbytbofngfcu_ukgpahdrcepdzbubh...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

2011/12/29 Manfred Lotz <manfred.l...@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/Control-Monad-Loops.html#v:allM

Regards Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111229/2f8ad4ea/attachment-0001.htm>

------------------------------

Message: 3
Date: Thu, 29 Dec 2011 04:49:53 -0500
From: Brandon Allbery <allber...@gmail.com>
Subject: Re: [Haskell-beginners] The numeric type stack
To: Mike Meyer <m...@mired.org>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <cakfcl4u9aj2ysvlq9yuebjjqyu8gmih3dbt2t3nq+3cx-ci...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Dec 29, 2011 at 00:33, Mike Meyer <m...@mired.org> wrote:

> So, is there a reasonable way to get the value of two Integral types
> divided by each other and rounded? How about one integral type and one
> RealFrac? I know I can get it truncated towards either 0 or negative
> infinity, but that's not what I want here.
>

You have two options:

(1) use fromIntegral to coerce an Integral value to a RealFrac;

(2) use (div) (division on Integrals) instead of (/).

Which to use depends on what you're planning to do with the result.  In
this case, I'd probably use (/) and (fromIntegral).

-- 
brandon s allbery                                      allber...@gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111229/cd48d6bd/attachment-0001.htm>

------------------------------

Message: 4
Date: Thu, 29 Dec 2011 10:59:46 +0100
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] question about any
To: Tim Baumgartner <baumgartner....@googlemail.com>
Cc: beginners@haskell.org, Manfred Lotz <manfred.l...@arcor.de>
Message-ID:
        <CANfjZRZO6fyfDtJHHq_MbSiFy=jJH0z6r7RisgHdm+=zjxl...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Thu, Dec 29, 2011 at 8:29 AM, Tim Baumgartner
<baumgartner....@googlemail.com> wrote:
> 2011/12/29 Manfred Lotz <manfred.l...@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/Control-Monad-Loops.html#v:allM
>

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?



------------------------------

Message: 5
Date: Thu, 29 Dec 2011 12:14:30 +0200
From: Markus L?ll <markus.l...@gmail.com>
Subject: Re: [Haskell-beginners] question about any
To: Manfred Lotz <manfred.l...@arcor.de>, beginners@haskell.org
Message-ID:
        <caldaiuaa-3kq3nfgwte6gfgsgqcqronrapqajeuth7pdann...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

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.l...@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



------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 42, Issue 31
*****************************************

Reply via email to