       Re: ReplaceAll (/.)

• To: mathgroup at smc.vnet.net
• Subject: [mg69200] Re: [mg69197] ReplaceAll (/.)
• From: "Christoph Lhotka" <lhotka at astro.univie.ac.at>
• Date: Fri, 1 Sep 2006 18:40:56 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Hello!

Rule (->) and RuleDelayed (:>) have a  different behaviour (like Set (=) and
SetDelayed (:=) when used with the replace operator:

With Rule you tell the Kernel first to evaluate the right hand side (e.g.
List@@x to x), then to use the rule. Thus your first example reduces to

(a b) /. x_Times -> x

which is indeed  (a b).

In the second example you tell the Kernel two hold the right hand side and
evaluate after pattern matching, thus you get, what you expected.

If you want deeper understanding use Trace wrapped around the two examples you
gave, to clarifiy the evaluation order.

According to your wish I suggest the following (shorter) code:

In[..]:= Times[a,b]/.Times->List
Out[..]:=List[a,b]

The basic idea of expressions in Mathematica allows you to modify both, the
Head and the arguments of the expression. You do not need a pattern here!

with kind regards
Christoph

On Fri, 1 Sep 2006 06:41:51 -0400 (EDT)
Bruce Colletti <vze269bv at verizon.net> wrote:
> Re Mathematica 5.2.0.0.
>
> Why doesn't:
>
>                (a b) /. x_Times -> List @@ x
>
> return {a,b}?  My (flawed) reasoning is that a b has form Times[a,b], and
> so x matches to Times[a,b].  List@@x replaces Times with List to yield
> List[a,b], or {a,b}...or so it would seem.
>
> The explanation is somehow linked to the fact that:
>
>                (a b) /. x_Times :> List @@ x
>
> returns {a,b}, but I don't see why this works but not the above.
>
> Although List@@(a,b) returns {a,b}, I want to use the above ReplaceAll
> function (this example here is taken from a larger problem).
>
> Thankx.
>
> Bruce
>

-- Mag. Christoph Lhotka --
University of Vienna / Institute for Astronomy
fon. +43 (1) 4277 51841
mail. lhotka at astro.univie.ac.at

```

• Prev by Date: Re: ReplaceAll (/.)
• Next by Date: RE: How to identify "intersections" between to data sets?
• Previous by thread: Re: ReplaceAll (/.)
• Next by thread: Re: ReplaceAll (/.)