MathGroup Archive 2008

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

Search the Archive

Re: Re: Variable amount of Buttons in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92882] Re: [mg92851] Re: Variable amount of Buttons in Mathematica
  • From: Fred Simons <f.h.simons at tue.nl>
  • Date: Thu, 16 Oct 2008 05:07:27 -0400 (EDT)
  • References: <gcscrf$8l3$1@smc.vnet.net> <gcv77d$ds9$1@smc.vnet.net> <200810150937.FAA11741@smc.vnet.net>

The problem discussed in this thread is caused by the HoldRest attribute 
of Button, and therefore is essentially the same as

In[1]:= Table[Hold[n], {n,1,5}]
Out[1]= {Hold[n],Hold[n],Hold[n],Hold[n],Hold[n]}

returning a list of 5 elements Hold[n].

Since Hold does not evaluate its argument, we have to *substitute* 
values for n in Hold[n] rather than assigning values to n.

Here is one solution (you might replace Hold with the Button you are 
interested in):

In[2]:= Table[Hold[n] /. n->i, {i,1,5}]
Out[2]= {Hold[1],Hold[2],Hold[3],Hold[4],Hold[5]}

The With-solutions are based on the fact that in the evaluation of 
With[{n=something}, argument] the first step is *substitution* of the 
value something for n in the expression argument; despite the syntax no 
assignments are involved.

In[3]:= Table[With[{n=n},Hold[n]], {n,1,5}]
Out[3]= {Hold[1],Hold[2],Hold[3],Hold[4],Hold[5]}

As can be seen from the syntaxcolouring, the second n in n=n is the 
counter of the Table command, while the first n is the vehicle that 
takes care of the substitution in the second argument of With.

Still another construction, which I have not seen mentioned, and easier 
to understand than the With construction, is based on the fact that 
mapping also uses substitution:

In[4]:= Hold[#]& /@ Range[5]
Out[4]= {Hold[1],Hold[2],Hold[3],Hold[4],Hold[5]}

A variant of this is

In[5]:= Array[Hold[#]&, 5]
Out[5]= {Hold[1],Hold[2],Hold[3],Hold[4],Hold[5]}

Kind regards,

Fred Simons
Eindhoven University of Technology


  • Prev by Date: Re: Newbie question: equations with sums.
  • Next by Date: Re: Newbie question: equations with sums.
  • Previous by thread: Re: Variable amount of Buttons in Mathematica
  • Next by thread: Re: Variable amount of Buttons in Mathematica