On Mon, 15 Jul 2002, Luke Palmer wrote:

> On Mon, 15 Jul 2002, Karl Glazebrook wrote:
>
> > @solution =  (^-@b + sqrt(@b^**2 ^+ 4^*@a^*@c) ) ^/ (2^*@a);
>
> That would not be very pretty, indeed.  It would also not be very
> efficient. (BTW, its b**2 - 4ac, not +  :)    A more efficient, pretty,
> and clear way would be like this:

Not necessarily true!  The compiler can figure out that a sequence of
operators is hyped and avoid the temporary arrays (like your for-loop).
It takes a little more work, but it's really not all that hairy when the
necessary information is right in the syntax.

Furthermore, the beauty of the hyper-operators (or the implicitly-vector
normal operators below) is they don't give the programmer a loop body in
which to do wierd things to the arrays.  Since

@y = $a ^* @x ^+ @b

is all a single operation from the user's perspective, the compiler can
make it faster than a loop written in pure Perl.

> > What is wrong with using @a * @b - the only reason I can think is to
> > preserve
> > a backward compatibility which is not needed?
> > ...
> > Why do we need to preserve @x as array length when you are proposing
> > @x.length ?
>
> I see your point.  I went through a couple of my larger perl programs, and
> the only time I used arrays in numeric context was in the C-style for
> loop.

I actually find myself using @a in numeric context quite a bit, but would
have no objections to changing it to @a.length, since the former is
completely bizarre to someone not familiar with Perl.

> I wouldn't mind if this proposal was accepted, but I also think the hyper
> operator syntax is nice.  At least for what I'm doing, in which it's
> usually Yet Another shorthand for a C<foreach> (or now just C<for>) loop.

Maybe we could add a hyper-context to cut down on the line-noise without
startling Perl 5 programmers:

@solution = ^( -@b + sqrt(@b**2 + 4*@a*@c) ) / (2*@a) );

Or (this might break something):

@solution ^= -@b + sqrt(@b**2 + 4*@a*@c) ) / (2*@a);

(then watch as people have all their inplace-array-bitwise-xors change
meaning;)

I agree that the ^op syntax is painful if you have to type it a lot, but
for the majority of people not using Perl for numerical code,
hyper-operation is something out of the ordinary, and seems to deserve
syntactic markers.  For example:

my @things = @args || @defaults; # @args if it is true, else @defaults
# vs
my @things = @args ^|| @defaults; # $args[$_] || $defaults[$_] for 0..X

If infix operators are implicitly vector, the first's becoming the second
could be a bit surprising.  Even more so since, if we're handling infinite
lists, hyper-operators should iterate over the number of items in their
shorter argument.

/s

Reply via email to