MathGroup Archive 2004

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

Search the Archive

Unexpected behaviour of HoldRest

Inspired by Carl Woll's OrderedUnion (for which many, many thanks!) I
have found similar uses for Sequence[], such as using replacement rules:

In[1]:= {{0,1,2,3,2},3,2} /. 2->Sequence[]

Out[1]= {{0, 1, 3}, 3}

But I recently found an "unexpected" result:

In[1]:= If[1 == 1, Sequence[], 2]

Out[1]= 2

Now when presented in this form it is "obvious", that Sequence[] is
being "flattened out" of the arguments to If.  The wanted behaviour of
If in this case can be restored if SequenceHold is applied:

In[3]:= Unprotect[If];
          SetAttributes[If, SequenceHold];

In[6]:= If[1 == 1, Sequence[], 2]

Out[6]= Sequence[]

But If has (I now find) the Attribute HoldRest, and Hold does not
flatten out Sequence[] so I don't understand why that does not apply
here also.

In[7]:= xx[1+2,2+3,3+4]

Out[7]= xx[3,5,7]

In[8]:= SetAttributes[xx,HoldRest]

In[9]:= xx[1+2,2+3,Sequence[],3+4]

Out[9]= xx[3,2+3,3+4]

Relatively few built-in functions have SequenceHold applied:

{AbsoluteTiming, Rule, RuleDelayed, Set, SetDelayed, TagSet,
TagSetDelayed, Timing, UpSet, UpSetDelayed}

And also relatively few built-in functions have HoldRest applied:

{Assuming, DumpSave, If, PatternTest, RuleDelayed, Save, Switch}

I will therefore now be much more careful of the use of Sequence!  At
present I don't want to be more daring and apply these attributes
wholesale without understanding why this is happening.  There must be
other cases where Sequence can have unexpected effects - I now
understand some of the problems I had when trying to use [mg13201] to
modify $Post -
reallyBig needs to cope with Sequence[] and Sequence[__].

 from-   John Tanner                email- john at
 mantra- curse Microsoft, curse...  web  -
I hate this 'orrible computer,  I really ought to sell it:
It never does what I want,      but only what I tell it.

  • Prev by Date: Re: StruveH runtime error
  • Next by Date: Re: Wolfram Functions Site
  • Previous by thread: Prob compiling MathLink code with Carbon
  • Next by thread: RE: Unexpected behaviour of HoldRest