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 2004

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

Search the Archive

Re: Optional Alternatives

  • To: mathgroup at
  • Subject: [mg50036] Re: Optional Alternatives
  • From: Bill Rowe <readnewsciv at>
  • Date: Thu, 12 Aug 2004 05:44:00 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

On 8/11/04 at 5:53 AM, sh2.7183 at (Selwyn
Hollis) wrote:

>Is it possible to require a function argument to match alternative
>patterns and be optional at the same time? As a toy example,
>suppose I define

>f[x:_Real|_Rational]:= x

The only way I know to achieve this is as follows:


>Is there a way make the argument optional with a given default
>value, say 0? I've tried this:

>f[Optional[x:_Real|_Rational, 0]]:= x

The following might do for you

In[1]:= g[x_, s_:0] := x + s

In[2]:= g[2]
Out[2]= 2

In[3]:= g[2, 2]
Out[3]= 4
To reply via email subtract one hundred and four

  • Prev by Date: Re: Re: How to set linewidth in 3-D graphics?
  • Next by Date: Re: How to set linewidth in 3-D graphics?
  • Previous by thread: SE and CI in NonlinearRegress
  • Next by thread: Re: Optional Alternatives