MathGroup Archive 2001

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

Search the Archive

Re: pattern matching quirks

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29362] Re: pattern matching quirks
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Fri, 15 Jun 2001 02:23:34 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <9g9n31$fr2$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

you can't give optional arguments *and* general pattern with :

But the definition


test[1, X["4"]] := X["4"]
test[1, inputs : X[X[_Integer]] ...] := inputs
test[1] := test[1, X["4"]]


should produce what you want.

Regards
  Jens

Michael wrote:
> 
> I am trying to write a function which matches only on a specific pattern of
> arguments, some of which can be optional.  The problem I am having is that
> certain complex patterns aren't being matched under conditions which should
> match.
> 
> For example:
> 
> In[1] := test[1, inputs : (X["4"] | X[X[_Integer]] ...) : X["4"]] := inputs
> 
> In[2] := test[1]
> Out[2] := X[4]
> 
> This is the expected behavior.  However if I redefine the function so that
> the alternatives are collected within a single head, it doesn't work:
> 
> In[3] := Clear[test];
> test[1, inputs : (X["4" | X[_Integer] ...]) : X["4"]] := inputs
> 
> In[4] := test[1]
> Out[4] := test[1]
> 
> In[5] := MatchQ[X["4"], (X["4" | X[_Integer] ...])]
> Out[5] := True
> 
> MatchQ says that my default values matches the given pattern.  Through some
> experimentation I had discovered that the function will NOT be called if the
> default value does not match the pattern for the optional argument.  This is
> annoying (and undocumented I think) because I would like to insert a
> function call to generate the default value.  This does not work right
> either because the default value appears to be evaluated when the function
> is defined, and wrapping it with Hold causes it to fail the above pattern
> match.  So instead I have to assign it to some inane value (hence the
> alternatives) and then do a rule transformation in the function itself (not
> shown above).
> 
> So here are my questions:
> 1. Why does the default value for a pattern have to match that pattern?
> 2. Why is the default value seeming to be evaluated when the function is
> defined?
> 3. Why do some patterns pass MatchQ but not result in a function call?
> Notably, X[a|b] versus X[a]|X[b]?
> 4. What other quirks should I be aware of that I haven't run into yet? :-)
> 
> Regards,
> 
> Michael Schmidt
> michael at science.edu


  • Prev by Date: Re: Re: Strange problems about my mathematica
  • Next by Date: Re: Truth in inequalities
  • Previous by thread: pattern matching quirks
  • Next by thread: Re: pattern matching quirks