Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*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 2000

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

Search the Archive

Re:Pattern match exactly 0 or 1 times?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg26424] Re:[mg26383] Pattern match exactly 0 or 1 times?
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Wed, 20 Dec 2000 00:21:45 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Gavin Scott wrote:
------------------------
Is there a way to specify a pattern which will match exactly zero or one
times? I find myself wanting somethinglike the standard regular expression
'?' operator, but can't see a way to do this with Mathematica's pattern
features. The closest I've been able to come is something like: x___ /;
x=="a" which will match zero or more copies of a specific value. I'd like to
match no more than one though. 
<snip>
-----------------------

One of the methods below should help.

In[1]:=
 Default[f]="a";
 f[x_.]:={x}
 {f[], f[3], f[3,4]}

Out[2]=
 {{a}, {3}, f[3,4]}



In[3]:=
 g[x_:"a"]:={x}
 {g[], g[3], g[3,4]}

Out[5]=
 {{a}, {3}, g[3,4]}


If you don't like one of the methods above you can of course have a separate
definition for no arguments.


In[6]:=
 h[]="a";
 h[x_]:={x}
 {h[], h[3], h[3,4]}

Out[8]=
 {a, {3}, h[3,4]}


------------------------
Regards,
Ted Ersek

Download Mathematica tips, tricks from 
http://www.verbeia.com/mathematica/tips/Tricks.html




  • Prev by Date: Re: How can I display the total time ?
  • Next by Date: Question: nonlinear differential equation with boundary conditions
  • Previous by thread: Re: Pattern match exactly 0 or 1 times?
  • Next by thread: Re: Pattern match exactly 0 or 1 times?