Re: [m1220] Equating a variable and its value
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1260] Re: [m1220] Equating a variable and its value
- From: Anthony Rebello <arebello at knox.edu>
- Date: Mon, 29 May 1995 03:08:51 -0400
On Fri, 26 May 1995, Jan Snellman wrote: > Hello folks. > > Study this codelet: > > m=3; mult=Times[#,m]&; Clear[m]; mult[10] > > To my dismay, I get 10 m, not 30. > How can i convince Mathematica that only pedants make the distinction > between a variable and its value? > > Regards, > Jan Snellman jans at matematik.su.se > > In[2]:= Attributes[Function] Out[2]= {HoldAll, Protected} The problem is that Function has the attriibute HoldAll, which means that nothing in the expression Times[#,m]& will be evaluated until after the function has been applied somewhere. So by clearing m before using mult you really are never letting the m in Times[#,m]& get replaced by 3. I tried several implementations of Evaluate and ReleaseHold but nothing would force the replacement of m early enough. The only solution seems to be to temporarily remove the HoldAll attribute from Function. Here is an example of how that could be done. In[16]:= ClearAttributes[Function,{Protected,HoldAll}] m=3; mult=Times[#,m]&; Clear[m]; mult[10] SetAttributes[Function,{Protected,HoldAll}] Out[17]= 30 NUMBERS-ARE-THE-ROOT-OF-ALL-EVIL..NUMBERS_A . R L E I Anthony Rebello | V Knox College T E Math Department H | E L | LA-FO-TOOR-EHT-ERA-SREBMUN.LIVE-LLA-FO-TOOR