Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: basic pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114343] Re: basic pattern matching
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Wed, 1 Dec 2010 02:14:26 -0500 (EST)
  • References: <id2mqa$isu$1@smc.vnet.net>

Hi,

> I'm confused regarding defaults.
> In[1]:= MatchQ[x,a_*x]
> Out[1]= False
> this has been expected
> 
> In[2]:= MatchQ[x,a_. *x]
> Out[2]= True
> this one too, but:
> 
> In[3]:= Cases[x,a_.*x:>a]
> Out[3]= {}
> should have been {1} shouldn't it? If the pattern matches (see
> Out[2]) then with a_. == 1. (Bug or feature?)

I think this has nothing to do with pattern matching but more with what
Cases actually looks at. The empty list results from the fact that Cases
works on level 1 by default, but the expression x has no level one so
Cases will return an empty list --- no matter which pattern you supply,
see e.g.:

Cases[x, _]

You can make Cases return what you expect by changing the level
specification:

Cases[x, a_.*x :> a, {0}]

> In[4]:= Cases[y+x,a_.*x:>a]
> Out[4]= {1}
> is OK.

here level 1 of the expression Plus[x,y] has to entries: y and x and the
second one matches, with the expected default value...

> Any enlightenment is highly welcome,


hth,

albert


  • Prev by Date: Re: basic pattern matching
  • Next by Date: Re: Re-virginating Manipulates?
  • Previous by thread: Re: basic pattern matching
  • Next by thread: Re: basic pattern matching