Specifications of 'any', 'all', 'findIndices'

Hannah Schroeter uk1o@rz.uni-karlsruhe.de
Tue, 23 Jan 2001 23:19:20 +0100


Hello!

On Tue, Jan 23, 2001 at 09:50:37AM +0000, Jerzy Karczmarczuk wrote:
> Hannah Schroeter wrote:

> > Eric Shade wrote:
> [...]

> > I don't think so. The specifications are quite concise.
> > Hannah.

> Just a moment, please. Do we speak about "concise" or "clear"?

I really probably spoke about "clear". However, I'm not a native
English speaker so I wasn't aware of the difference between those
two words until just now, when I looked them up.

In this case, I find
  any p = or . map p
rather clear, and almost as concise as it can get (any = (or .) . map even
is shorter, thus perhaps more concise, but IMHO less clear for people
not so apt in a combinatorial style).

> [...]

> "off-line".) THREE TIMES I've been asked about that. Somebody
> quite clever remarked that any or all are *typical* cases for
> fold rather than for map.

You / they probably mean something like this:
  any p = foldr (\ elem temp_res = p elem || temp_res) False

> [...]

> Please, don't speculate. If you have something to say in this
> context, perform some tests. I did it with Hugs. Eric Shade
> implementation seems to be indeed more efficient, but very
> slightly (on my test, I won't claim anything general).

Hugs is only a good test for --- hugs :-)

However, just tested with GHC and results were these:

hannah@c3po:/tmp $ cat x.hs
my_any p = or . map p

main =
        print (my_any (== 0) [1..10000000])
hannah@c3po:/tmp $ time ./x
False

real    0m6.003s
user    0m5.917s
sys     0m0.024s

hannah@c3po:/tmp $ cat y.hs 
my_any p [] = False
my_any p (x:xs) = p x || my_any p xs

main =
        print (my_any (== 0) [1..10000000])
hannah@c3po:/tmp $ time ./y
False

real    0m4.403s
user    0m4.330s
sys     0m0.016s

GHC was 4.08.

Kind regards,

Hannah.