Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15989

Protect a variable against being used as an iterator (related to the HoldAll - Evaluate problem)

From Guido Walter Pettinari <coccoinomane@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Protect a variable against being used as an iterator (related to the HoldAll - Evaluate problem)
Date 2011-02-02 11:13 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iibe84$nts$1@smc.vnet.net> (permalink)

Show all headers | View raw


Dear Mathematica group,

I would like to ask you a simple question that, if answered, would
make my life much easier :-)

Does anybody know how to trigger a warning/error message whenever a
particular symbol is used as an iterator? By iterator, I mean the
second argument of functions like Plot, Table, Sum, i.e. the "i" in
Table [ i^2,  {i, 10} ]

I am looking for this feature since I get errors/unmeaning results
whenever I use as an iterator a variable already defined in some other
part of the code (which I may have forgot of).
E.g., this happens with NDSolve.  Take the following example:

tmin = 0;
tmax = 1;
sol[a_] := NDSolve [ {y'[t] == a y[t], y[0] == 1},  y,  {t, tmin,
tmax} ]
exp[a_, t_] := y[t] /. sol[a]

The following Plot command does not work:
Plot[exp[2, t], {t, tmin, tmax}]
while changing the iterator name works:
Plot[exp[2, x], {x, tmin, tmax}]

I know that (i) the first Plot command does not work because Plot
(like Table and Sum) has the HoldAll attribute, and (ii) using the
Evaluate function on exp[2, t] solves the problem.

However, say that I publish a package that uses a variable in the same
way I use "t" in the above example.  How does the user of the package
know that she should not use that variable to iterate?  It would be
nice if she gets a warning message whenever she tries to do so.

I guess that a workaround would be to use a Unique[] symbol, either as
an iterator or as, say, the NDSolve independent variable.... do you
think this is doable?

Thank you very much!

Regards,

Guido

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Protect a variable against being used as an iterator (related to the HoldAll - Evaluate problem) Guido Walter Pettinari <coccoinomane@gmail.com> - 2011-02-02 11:13 +0000

csiph-web