MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Uniform design


Here's another issue that I think belongs to this thread: it's
interesting to consider how well Table/Sum double as both mathematical
and programming constructs. They work like Block to make possible
things like y=x;Table[y,{x,5}], but how well is this approach suited
for programming? Suppose we're writing something like

f[L_]:=Table[L[[i]],{i,Length@L}]

to get the list of elements of L (a clumsy way to write List@@L); then
everything works fine until we try f[{i,j}]. So for programming this
block-like behaviour is more like a hidden hazard; it works well only
if we know the structure of L in advance. Instead, what is needed here
is a Table/Sum with a local (unique) iterator; otherwise we need to
wrap every Table/Sum in Module or put it in a separate context.

Besides, there's a matter of 'optimizations' that Mathematica uses for
Sum (an issue which was discussed in this newsgroup a while ago):

In[1]:=
Module[{cnt = 0}, Sum[cnt += 1, {i, 10^6}] ]
Module[{cnt = 0}, Sum[cnt += 1, {i, 10^6 + 1}] ]

Out[1]=
500000500000

Out[2]=
1000001

When using Sum as a programming construct, probably the last thing
user needs is to see it unexpectedly change behaviour after reaching
some internal threshold.

Maxim Rytin
m.r at prontomail.com


  • Prev by Date: Re: Extracting Coefficients and Powers
  • Next by Date: Re: Extracting Coefficients and Powers
  • Previous by thread: Re: Uniform design
  • Next by thread: Re: Uniform design