Re: a question about pattern replacements
- To: mathgroup at smc.vnet.net
- Subject: [mg36172] Re: [mg36163] a question about pattern replacements
- From: Andrzej Kozlowski <andrzej at lineone.net>
- Date: Mon, 26 Aug 2002 04:15:49 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
You are quite right, Mathematica does evaluate Flatten[q] before substituting {{1, 2}, {3, 4}}. It then issues the error message and returns Flatten[q]. Only now {{1, 2}, {3, 4}} is substituted for q, Flatten[{{1, 2}, {3, 4}}] is evaluated and you get the right answer. To avoid all this just use RuleDelayed instead of Rule: In[1]:= z := SomeHead[{{1, 2}, {3, 4}}]; In[2]:= z /. {SomeHead[q_] :> Flatten[q]} Out[2]= {1,2,3,4} Andrzej Kozlowski Toyama International University JAPAN On Saturday, August 24, 2002, at 02:34 AM, Sidney Cadot wrote: > Hi all, > > While playing arounf with patterns and substitutions, I came across the > following behavior which I didn't expect: > > 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]. > > ........as if it is trying to evaluate Flatten[q], with q not bound to > {{1,2},{3,4}}. > > Could anybody explain why this happens? > > Best regards, > > Sidney Cadot > > > > > >