MathGroup Archive 2010

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

Search the Archive

Re: basic pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114335] Re: basic pattern matching
  • From: Armand Tamzarian <mike.honeychurch at gmail.com>
  • Date: Wed, 1 Dec 2010 02:12:52 -0500 (EST)
  • References: <id2mqa$isu$1@smc.vnet.net>

On Nov 30, 10:22 pm, Peter Pein <pet... at dordos.net> wrote:
> Dear group,
>
> 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?)
>
> In[4]:= Cases[y+x,a_.*x:>a]
> Out[4]= {1}
> is OK.
>
> Any enlightenment is highly welcome,
> thanks in advance, Peter
>
> P.S.: $Version is 8.0 for Linux 64-bit (November 7, 2010)

The differences in the Cases behaviour you are seeing are due to the
differences in the depth of the Cases arguments.

Depth[x] is 1, Depth[y+x] is 2. Also Depth[{x}] is 2. So

In[3]:= Cases[x,a_.*x:>a]
Out[3]= {}

This is expected, but what you want is:

In[3]:= Cases[{x},a_.*x:>a]
Out[3]= {1}

Mike


  • Prev by Date: Re: How to assume that a function is positive?
  • Next by Date: Re: basic pattern matching
  • Previous by thread: Re: basic pattern matching
  • Next by thread: Re: basic pattern matching