A little challenge...

Norbert Nemec Norbert at Nemec-online.de
Thu Feb 25 15:57:32 PST 2010


Hi everybody,

thinking about array expressions, I have stumbled over an interesting 
challenge for which I still have no idea:

Consider the mathematical sum notation:

	\sum_i a_i*b_i

here, the variable i is defined only at the scope inside the expression.

A analogous D syntax could be something like

	sum!(i)(a[i]*b[i])

where sum would have to be some kind of template that takes i as a name 
parameter and then defines it as variable inside the scope of the second 
expression.

(Of course, the scalar product is trivial enough to do it in a zillion 
of other ways, but for more complex sum expressions with potentially 
more than one variable, this would really be neat to have in an array 
library.)

Does anyone have an idea how this could be realized in D? Allowing a 
template to define additional symbols only for the scope of its 
arguments? Is it possible at all with the current language definion? 
Does anyone have an idea for an elegant language extension to allows 
this? It would mean taking lazy evaluation even one step further, all 
the way to a lazy symbol binding. Seems straighforward enough to me to 
implement in a compiler. Just the question how exactly to define the 
semantics.

Greetings,
Norbert



More information about the Digitalmars-d mailing list