MathGroup Archive 2004

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

Search the Archive

RE: ReplaceList -- Unexpected Answer

  • To: mathgroup at
  • Subject: [mg46805] RE: [mg46788] ReplaceList -- Unexpected Answer
  • From: "David Park" <djmp at>
  • Date: Tue, 9 Mar 2004 04:30:48 -0500 (EST)
  • Sender: owner-wri-mathgroup at


Because ReplaceList is not working the way you are thinking. It only applies
the rule to the ENTIRE expression.

However, in the ReplaceList Examples the following routine is given...

ReplaceAllList[expr_, rules_] :=
    Join[ReplaceList[expr, rules],
      If[AtomQ[expr], {},
        Join @@ Table[
            ReplacePart[expr, #, i] & /@ ReplaceAllList[expr[[i]], rules],

ReplaceAllList[ {f[h[4], h[4]], f[h[4], h[5]]}, f[x : h[_], x_] -> r[x] ]
{{r[h[4]], f[h[4], h[5]]}}

David Park
djmp at

From: Harold Noffke [mailto:Harold.Noffke at]
To: mathgroup at


In The Mathematica 5 Book, Section 2.3.3 Naming Pieces of Patterns, we
find the following pattern matching exercise ...

	Now both arguments of f are constrained to be the same, and only the
	first case matches.

		{f[h[4], h[4]], f[h[4], h[5]]} /. f[x:h[_], x_] -> r[x]

Now, let's use ReplaceList to get more insight into this matching
process ...

		ReplaceList[ {f[h[4],h[4]],f[h[4],h[5]]},f[x:h[_],x_] -> r[x] ]

I do not understand why ReplaceList returns {} instead of { r[h[4]] }.


  • Prev by Date: Re: ReplaceList -- Unexpected Answer
  • Next by Date: Re: Problem with Mathematica driving me nuts
  • Previous by thread: Re: ReplaceList -- Unexpected Answer
  • Next by thread: RE: ReplaceList -- Unexpected Answer