Re Need a new type of Hold
- To: mathgroup at smc.vnet.net
- Subject: [mg7675] Re [mg7538] Need a new type of Hold
- From: Dick Zacher <dick at loc3.tandem.com>
- Date: Thu, 26 Jun 1997 01:36:58 -0400 (EDT)
- Organization: Tandem Computers
- Sender: owner-wri-mathgroup at wolfram.com
Ersek_Ted%PAX1A at mr.nawcad.navy.mil wrote: > > In [mg7538] Dave Wagner replied to my desire for a new type > of Hold .... My original correspondence was [mg7499]. > > I recently figured out how to change HoldForm so that it works pretty > much the way I want the new type of Hold to work. > The only problem is that I lose the usual implementation of HoldForm > along the way. > > Can someone out there build this feature without messing up HoldForm. > > (*-------------------------------------------------*) > > In[1]:= Unprotect[HoldForm]; > HoldForm/:g_[a___,HoldForm[expr_],b___]/; > (g=!=FullForm && g=!=Set && g=!=SetDelayed && > g=!=List && g=!=Part) := g[a, expr, b] > > (* "a___" and "b___" are BlankNullSequences *) > > In[2]:= foo=HoldForm[z+2+d+a+3] > > Out[2]= z+2+d+a+3 > > In[3]:= foo-d+4 > > Out[3]= 9+a+z > > In[4]:= HoldForm[z+2+d+a+3] //FullForm > > Out[4]//FullForm= > HoldForm[Plus[z, 2, d, a, 3]] > > In[5]:= foo+b > > Out[5]= 5+a+b+d+z > > (*________________________*) > ... > ... > Some important points: > - This should work on any expression, not only those with Plus for a Head. > - I want to use the held results later on without releasing the hold. > - When the held result is used in later calculations, the hold should be > released automatically. > ... > ... I couldn't resist working on this one, and learned a lot from it; so I'll share my own attempt at a solution. It is based on Ted's solution, above, but suppresses the output of HoldTemporary via an additional definition for MakeBoxes rather than by redefining HoldForm. Because of this, it's only useful for Mma 3.0 or later. (*v v v v v v v v v v v v v v v v v v v*) SetAttributes[HoldTemporary,HoldAll] mb:MakeBoxes[_,_]:= ReleaseHold[( Hold[mb]//.{HoldPattern[HoldPattern[HoldTemporary[e_]]]:>e})]/; !FreeQ[Hold[mb],HoldPattern[HoldPattern[HoldTemporary[_]]]] HoldTemporary/:g_[a___,HoldTemporary[e_],b___]:= g[a,e,b]/;g=!=Set&&g=!=FullForm (*^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^*) We won't go into how long it took me to figure out the need for those nested HoldPattern functions. The above definitions show the following behavior: (*v v v v v v v v v v v v v v v v v v v*) In[4]:= foo=HoldTemporary[z+2+d+a+3] Out[4]= z+2+d+a+3 In[5]:= foo-d+4 Out[5]= 9+a+z In[6]:= HoldTemporary[z+2+d+a+3] //FullForm Out[6]//FullForm= Plus[z,2,d,a,3] In[7]:= foo+b Out[7]= 5+a+b+d+z (*^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^*) Since I don't really understand the application, I don't know how well this solution meets the requirements; but it seems to work on the examples given, and I don't think it breaks any other functions. -- ----------------------------- Dick Zacher Performance Engineering Dept., Tandem Computers zacher_dick at tandem.com phone: 408-285-5746 fax: 408-285-7079