MathGroup Archive 2010

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

Search the Archive

Re: Variables in Iterator limits?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109679] Re: Variables in Iterator limits?
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Tue, 11 May 2010 06:29:28 -0400 (EDT)
  • References: <hs3gic$rt2$1@smc.vnet.net>

AES wrote:
> If I execute the following lines, I get the graphic that I want:
> 
>    x; Remove["Global`*"];
> 
>     lines := Graphics[ Table[ Line[{{k 10/kmax,-1},
>                                      {k  10/kmax,1}}], {k,1,kmax}]]
> 
>    km = 30; Show[lines]
> 
> If I execute the following lines, I get exactly the same graphic, all OK,
> 
>    x; Remove["Global`*"];
> 
>    lines := Graphics[ Table[ Line[{{k 10/kmax,-1},
>                                      {k  10/kmax,1}}], {k,1,kmax}]]
> 
>    testValues = {kmax->30};  Show[lines] /. testValues
> 
> -- except that I also get a beep, and an error msg in the Messages 
> window:
> 
>      "Iterator {k,1,kmax} does not have appropriate bounds"
> 
> (but no little red square in the graphics output cell).
> 
> Same thing happens in the /. case if I do the two tests in reverse order.
> 
> Seems like another annoying little Mathematica "gotcha" to me . . . ?
> 

Clearly the error message arises from the attempt to execute the expression:

Table[ Line[{{k 10/kmax,-1},{k  10/kmax,1}}], {k,1,kmax}]

The left hand side of ReplaceAll is evaluated before the ReplaceAll is 
executed - which surely makes sense!

Therefore, are you suggesting that expressions should never generate 
errors if they cant be evaluated - just left unevaluated on the off 
chance that a subsequent replacement will leave something that could be 
evaluated!

Imagine how frustrating it would be to use a system like that!

Obviously Mathematica is not going to change in this regard, but what on 
earth would you prefer it to do?

I generally avoid using SetDelayed to set the value of individual 
variables (as opposed to functions) precisely because the order of 
evaluation can surprise and anyway mathematical intuition leads one to 
expect a variable to be no more than a repository for a value.

David Bailey
http://www.dbaileyconsultancy.co.uk


  • Prev by Date: Re: How to write reports and books in Mathematica
  • Next by Date: Re: How to write reports and books in Mathematica
  • Previous by thread: Re: Variables in Iterator limits?
  • Next by thread: How to Explain This Behavior of Simple Procedures?