A little challenge...
Robert Jacques
sandford at jhu.edu
Thu Feb 25 18:54:30 PST 2010
On Thu, 25 Feb 2010 18:57:32 -0500, Norbert Nemec
<Norbert at nemec-online.de> wrote:
> 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
Well there is std.algorithm's map and reduce. Somehow, I feel slicing,
ranges/generators and array expressions should be able to handle this. For
example: \sum_i i*a_i*b_i-1 => sum(iota * a * b[1..$]) But then again I'm
having trouble thinking of real examples off the top of my head.
More information about the Digitalmars-d
mailing list