Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Returning an empty sequence

  • To: mathgroup at
  • Subject: [mg63306] Re: Returning an empty sequence
  • From: "Jean-Marc Gulliet" <jeanmarc.gulliet at>
  • Date: Fri, 23 Dec 2005 05:08:30 -0500 (EST)
  • Organization: The Open University, Milton Keynes, U.K.
  • References: <dode3g$62d$>
  • Sender: owner-wri-mathgroup at

"Trevor Baca" <trevorbaca at> a écrit dans le message de news: 
dode3g$62d$1 at
| What's the right way to have an expression evaluate to an empty
| sequence?
| I'd like ...
| f[n_] :=
|  Switch[n,
|    1, "foo",
|    2, Sequence[ ],
|    3, {"bar"}];
| ... to work, but instead I get the message
| Switch::"argct": "Switch called with 6 arguments. Switch must be called
| with an odd number of arguments."
| meaning that the Sequence[ ] is flattened in the rhs definition of f[
| ].
| Is there a way to get f[ ] to evaluate to the empty Sequence[ ] (rather
| than Null)?
| Trevor.

Hi Trevor,

You could add the attribute *SequenceHold* to the built-in function *Switch* 
as in the following:

In[1]:= f[n_]:=Switch[n,1,"foo",2,Sequence[],3,{"bar"}];

In[2]:= Attributes[Switch]
Out[2]= {HoldRest,Protected,ReadProtected}

In[3]:= Unprotect[Switch];

In[6]:= Attributes[Switch]
Out[6]= {HoldRest,Protected,ReadProtected,SequenceHold}

In[7]:= f[2]
Out[7]= Sequence[]

Best regards,

  • Prev by Date: Re: Taking either a sequence or a list
  • Next by Date: Re: Multiple integration of UnitStep fails
  • Previous by thread: Re: Returning an empty sequence
  • Next by thread: Gaussian sums (Was: Speeding up simple Mathematica expressions?)