Re: a question about pattern replacements
- To: mathgroup at smc.vnet.net
- Subject: [mg36177] Re: a question about pattern replacements
- From: Tom Burton <tburton at brahea.com>
- Date: Mon, 26 Aug 2002 04:15:54 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 8/23/02 6:46 PM, in article ak6ope$e9q$1 at smc.vnet.net, "Sidney Cadot"
<sidney at jigsaw.nl> wrote:
> z := SomeHead[{{1, 2}, {3, 4}}]
> z /. {SomeHead[q_] -> Flatten[q]}
>
> While this _does_ yield the desired result {1,2,3,4}, Mathematica complains:
>
> Flatten::"normal": Nonatomic expression expected at position 1 in
> Flatten[q].
The -> operator is prompt: It permits evaluation of the RHS Flatten[q]
immediately, before it is used in the substitution. Evidently, q was
undefined before you tried this, so Flatten[q]'s evaluation yielded itself.
Later, in the process of substitution, with q set as you intended, Flatten
evaluated again, this time yielding the desired result.
Try it again after setting q to something. Then you get not only the
complaint but also the wrong answer.
To avoid these issues, use the delayed operator :> instead of the prompt
operator ->. A rule of thumb is: Use delayed operators := and :> when the
LHS (SomeHead[q_] in your case) contains a blank (_).
Tom Burton