MathGroup Archive 1999

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

Search the Archive

Scoping and named patterns

  • To: mathgroup at
  • Subject: [mg18132] Scoping and named patterns
  • From: Dr Dan <drdanw at>
  • Date: Fri, 18 Jun 1999 00:51:38 -0400
  • Organization: - Share what you know. Learn what you don't.
  • Sender: owner-wri-mathgroup at

I am having trouble with name conflicts between global symbols and
named patterns.

This example from The Book works fine:

In[1]:= f[a^b] /. f[x : _^n_] -> p[x, n]
Out[1]= p[a^b, b]

But if the symbols used as pattern names have values:

In[3]:= n = 2; x = 3;
        f[a^b] /. f[x : _^n_] -> p[x, n]
Out[3]= p[3, 2]

My usual favorite scoping structure, Module, doesn't help:

In[4]:= Module[{x, n}, f[a^b] /. f[x : _^n_] -> p[x, n]]
Out[4]= p[3, 2]

This shows that the global symbol is used as the pattern name and not
the symbol local to the scoping construct:

In[5]:= Module[{x, n}, Clear[x, n]; f[a^b] /. f[x : _^n_] -> p[x, n]]
Out[5]= p[3, 2]

Since local symbols are ignored, it is necessary to use Block:

In[6]:= Block[{x, n}, f[a^b] /. f[x : _^n_] -> p[x, n]]
Out[6]= p[a^b, b]

This looks like a bug to me.  If I use a symbol in a local context I
expect the local symbol and never the global.  I am a little concerned
that the pattern itself doesn't scope its pattern names, that I can
make one seemingly small change in my notebook and my patterned
replacements begin crashing.

Any comments, or a better workaround than Block?

Sent via
Share what you know. Learn what you don't.

  • Prev by Date: Generating self-affiine fractals
  • Next by Date: why Developer` and Experimental` functions are not in Add-ons help
  • Previous by thread: Generating self-affiine fractals
  • Next by thread: Re: Scoping and named patterns