I'm a bit unsure of where to put this... it's a question of implementation,
but of a feature that is still being discussed still in perl6-language...
Let me know if this is the wrong forum or the wrong time...

There's been a lot of discussion about reduce/fold on perl6-language. The
details are still being decided (and Damian Conway is doing an RFC on this
and similar topics), but it's likely that something like this will be
doable:
$a = sum(@b*@c+@d)
where the lists are all 1-dimensional arrays, here, so $a ends up as the sum
of {the component-wise multiplication of @b and @c, added component-wise to
@d}.

The normal problem with this type of structure is that the previous
statement would create 2 array copies, and 3 loops for most compilers. In
perl speak, it might look like:
$dummy1[$_] = $b[$_]*$c[$_] for (0..$#b-1);
$dummy2[$_] = $d[$_]+$dummy1[$_] for (0..$#dummy1-1);
$sum+=$_ for (@dummy2);
(Sorry if this isn't very idiomatic perl--it's not really my native
language.)

Progressive C++ numeric programming libraries like POOMA and Blitz++ use
template meta-programming techniques to implement 'expression templates'.
Templates are used to create the parse tree for these kind of array
expressions at compile time, and the compiler then optimises out the extra
loops and array copies to create something like:
$sum+=$b[$_]*$c[$_]+$d[$_] for (0..$#b-1);

Without this optimisation, array semantics become next to useless for
numeric programming, because their overhead is just so high. But writing
numericly intensive programs without array semantics is messy--they become
littered with control structures and loops (which is particularly
unintuitive for mathmaticians used to the compact notation of mathematics).

So, could perl 6 do this optimisation (assuming that the array
notation/folding stuff makes its way into the language)? Given that some
amount of compilation or interpretation will presumably still be done at
run-time, perhaps this is much easier in perl than in C++...


Reply via email to