MathGroup Archive 2011

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116107] Protect a variable against being used as an iterator (related to the HoldAll - Evaluate problem)
  • From: Guido Walter Pettinari <coccoinomane at gmail.com>
  • Date: Wed, 2 Feb 2011 06:09:05 -0500 (EST)

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


  • Prev by Date: Re: some demonstrations no longer work right in version 8
  • Next by Date: Re: FindFit bug
  • Previous by thread: Re: Read last expression in a file?
  • Next by thread: Re: Protect a variable against being used as an iterator (related to the HoldAll - Evaluate problem)