Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Unexpected behaviour of HoldRest

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45678] Unexpected behaviour of HoldRest
  • From: John Tanner <john at janacek.demon.co.uk>
  • Date: Mon, 19 Jan 2004 05:15:21 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

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];
          Protect[If];

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 janacek.demon.co.uk
 mantra- curse Microsoft, curse...  web  - http%##www.janacek.demon.co.uk/
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