MathGroup Archive 2006

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

Search the Archive

Re: ReplaceAll (/.)

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


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

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

On Fri, 1 Sep 2006 06:41:51 -0400 (EDT)
 Bruce Colletti <vze269bv at> wrote:
> Re Mathematica
> 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

  • 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 (/.)