MathGroup Archive 2010

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

Search the Archive

Re: precedence for ReplaceAll?

On 6/26/10 at 3:12 AM, siegman at (AES) wrote:

>In article <i023lj$r3c$1 at>,
>Albert Retey <awnl at> wrote:

>>No, what actually happens is that a and b are evaluated to A*c and
>>B*c respectively, even before the ReplaceAll sees them. Then the
>>ReplaceAll sees

>>A*c+B*c /. {A*c -> x, B*c -> y} A*B*c^2 /. {A*c -> x, B*c -> y}

>>where it will find matches in the first case but not in the second
>>and so does nothing.

>I'm guessing that in the initial evaluation stage(s) referred to
>above, any and all "superfluous" parens are also stripped out, so
>that even if the OP had started with

>a := (A c)
>b := (B c)

>(and maybe inserted protecive parens at various places in subsequent
>line, the results would have been exactly the same . . . ?

Yes, the result would be the same. Try it and see. If this were
not the case, there would be a serious bug.

>Would there be a place in Mathematica for a "FindAndReplaceText[-]"
>command that would do the equivalent of a word processor's "Find and
>Replace" operation ***on the literal text string that was typed into
>a Mathematica cell*** (or that appeared on screen in an Output
>cell), without doing any evaluation at all, before or afterwards?

There is a FindAndReplaceText already in Mathematica. It is
called StringRreplace and operates on string (text) data. But
keep in mind, most things you input into Mathematica are not
strings, not represented as literal text when you enter them.

>Thinking that this is what ReplaceAll does (or should do) is
>obviously the source of a lot of misinterpretation and trouble for
>Mathematica users.  But maybe defining such a command, and
>determining what it should and shouldn't do would be too difficult
>or too complicated to be realistically possible.

ReplaceAll does do what you have described. That is replaces all
instances of a target with something else as specified. The
difficulty new users have with ReplaceAll is that it operates on
the form of the expression Mathematica retains internally which
is often not what a user expects based on what is displayed.

  • Prev by Date: Automatic Differentiation of mathematica code
  • Next by Date: Re: and sampling a distribution
  • Previous by thread: Re: precedence for ReplaceAll?
  • Next by thread: Re: precedence for ReplaceAll?