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

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

Search the Archive

Match exactly zero or one

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60518] Match exactly zero or one
  • From: "Trevor Baca" <trevorbaca at gmail.com>
  • Date: Mon, 19 Sep 2005 04:45:38 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

What's the easiest way to match exactly zero or one expressions within,
say, MatchQ[ ]?

For example: call any expression expr a well-formed foo when and only
when (1) the head of expr equals foo; (2) the first element of expr is
either 0 or 1 bar expressions; (3) the last element of expr is an
integer. Then neither the  predicate ...

   FooQ[expr_] := MatchQ[expr, foo[_bar, _Integer]]

... nor ...

   FooQ[expr_] := MatchQ[expr, foo[__bar, _Integer]]

... nor ...

   FooQ[expr_] := MatchQ[expr, foo[___bar, _Integer]]

... accurately capture the definition of foo.

For string-matching there's the ? operator; but what's the best
approach for expression-matching?

(FWIW Gavin Scott posed this question in December 2000, but I couldn't
find an answer in the thread.)


Trevor.


  • Prev by Date: Re: Outputting MatrixForm to Word
  • Next by Date: Re: integral to table values
  • Previous by thread: Re: Differences between recursions and limit
  • Next by thread: Re: Match exactly zero or one