On Thu, Aug 10, 2000 at 07:22:21PM -0400, Chaim Frenkel wrote:
> Okay, then for
>
> reduce &avg $identity, @list
>
> What should $identity be?
I would like to see what &avg would be, given that each time it
would be passed the previous result (or the first element) and
the next element.
So without using an external variable to keep count of how many
elements had been processed so far it could not be done. So you
would probably do it like
(reduce __+__ 0, @list) / @list.
This will ensure that the sum is defined.
However this is probably just a bad exanple of why not to require an
identity element.
A better example might be trying to perform join, with reduce eg
reduce __.','.__ $identity, @list
Which would not work for any value of $identity unless you did
reduce __.','.__ shift @list, @list
which is probably a good example why not to require an identity element.
Graham.
> >>>>> "AS" == Ariel Scolnicov <[EMAIL PROTECTED]> writes:
>
> AS> Think of the first element of the list as different from the rest --
> AS> it is the initial value to reduce from (for + and *, you'll usually
> AS> pick an appropriate identity element). By writing
>
> AS> @sum = reduce __+__ 0, @numbers
>
> AS> you deal elegantly with both cases.
>
> AS> NOTE: I find this trick very elegant. I wish it were my trick,
> AS> instead of Damian's...
>
> --
> Chaim Frenkel Nonlinear Knowledge, Inc.
> [EMAIL PROTECTED] +1-718-236-0183
>