Re Need a new type of Hold
- To: mathgroup at smc.vnet.net
- Subject: [mg7623] Re [mg7538] Need a new type of Hold
- From: Ersek_Ted%PAX1A at mr.nawcad.navy.mil
- Date: Fri, 20 Jun 1997 16:15:57 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In [mg7538] Dave Wagner replied to my desire for a new type
of Hold (see below). 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
(*________________________*)
Originally I didn't want to have any type of Hold to show up in the
FullForm.
I couldn't make it work that way, but that isn't important.
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.
In the above I included ( g=!=SetDelayed && g=!=List && g=!=Part ).
This isn't needed for the performance I wanted earlier, but I thought it was
an enhancement.
Ted Ersek
ersek_ted%pax1a at mr.nawcad.navy.mil
--------------------------------------------------------
Dave Wagner replied:
>At first I thought that what you really want to do is define your
>own addition operator, but then I decided that what you are trying to
>do is fundamentally inconsistent with the way
>Mathematica works (I won't say "impossible", since everytime I say
t>hat some smart-aleck figures out a way to do it.)
>
>Here's why:
i>n your first output, the 2 and the 3 don't combine. Yet, in the second
>output, they have combined with the 9. What should happen if I type
>"foo + b"? Should the result be "z+2+d+a+3+b" or should it be
>"5+a+d+a+b". Or should it be something else? The fact is, if you want
>the 2 and 3 to combine, then they should have combined in the first
>output; if you want the order of the terms to be strictly maintained,
>then the second output should have been "z+2+d+a+7" (let's assume that
>it's okay to combine adjacent terms). One or the other, you can't
>have it both ways.
>
>By the way, if it's a non-associative addition operator that you're
>after, I don't recommend removing the Orderless attribute from Plus,
>or you'll break a lot of other things.
>
> Dave Wagner
> Principia Consulting