Chaim Frenkel <[EMAIL PROTECTED]> writes:

> >>>>> "AS" == Ariel Scolnicov <[EMAIL PROTECTED]> writes:
> 
> >> If the original list has no elements, C<reduce> immediately returns C<undef>.
> 
> AS> I like everything except this part.  Reducing an empty list should be
> AS> an error.
> 
> AS> Returning undef (or anything else, really) breaks the algebraic
> AS> equivalence that
> 
> AS>             f((reduce \&f, LIST), $x) eq (reduce \&F, LIST, $x)
> 
> I don't see it.
> 
>       1 == f((reduce +, undef), 1) == reduce +, undef, 1

You're confusing "f()" and "+" here, and (on the LHS) "undef" with
"()".  What you're claiming is that

        f(undef, 1) eq 1

for any function f().  While this is true (in Perl) for C<__+__>, it's
not even true for C<__*__>.  And of course there's nothing you can do
about user-specified functions f().

> I really would like to be able to pass around an empty list and get
> a _reasonable_ answer. Having to check the list for emptyness before
> passing it in just seems dirty. One doesn't have to do this for anyother
> looping operator in the language.
> 
>       for (@empty) {}
>       grep {} @empty
>       map {} @empty
> 
> etc.

Think of the first element of the list as different from the rest --
it is the initial value to reduce from (for + and *, you'll usually
pick an appropriate identity element).  By writing

        @sum = reduce __+__ 0, @numbers

you deal elegantly with both cases.

NOTE: I find this trick very elegant.  I wish it were my trick,
instead of Damian's...

[...]

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            | [EMAIL PROTECTED]
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem) \ We recycle all our Hz
72 Pinhas Rosen St.    |Tel: +972-3-7658514 (Main office)`---------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555    http://3w.compugen.co.il/~ariels

Reply via email to