MathGroup Archive 2002

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

Search the Archive

Using a Named Pattern in a Rule

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36278] Using a Named Pattern in a Rule
  • From: "David Park" <djmp at earthlink.net>
  • Date: Thu, 29 Aug 2002 01:38:06 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I would like to match a named pattern in an expression and then square the
result. But my attempt fails.

Clear[f, g, x, y, a]
expr = 3*f[x]*g[y] + 2 + x^2;

expr /. a:(f[_]*g[_]) :> a^2
2 + x^2 + 3*f[x]*g[y]

If I drop the name on the pattern, it matches - but it doesn't do what I
want.

expr /. f[_]*g[_] :> a^2
2 + 3*a^2 + x^2

How can I name such a pattern and use it on the rhs of a rule?

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/



  • Prev by Date: Re: How to protect the Mathematica Program
  • Next by Date: Re: ReadList and NumberPoint ?
  • Previous by thread: Re: Transpose problem
  • Next by thread: Re: Using a Named Pattern in a Rule