From: David Park on 11 Feb 2010 07:12 Because the Or gets evaluated on the lhs of the rule. Use HoldPattern. x = Or[a, b]; x /. HoldPattern[Or[y__]] -> {y} {a, b} But notice: Or[y__] -> {y} y__ -> {y} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Istv=E1n Zachar [mailto:zac(a)freemail.hu] Can you please explain why ReplaceAll does not return a simple list? In[21]:= ClearAll[x, a, b]; x = Or[a, b]; FullForm@x Out[23]//FullForm= \!\(\* TagBox[ StyleBox[ RowBox[{"Or", "[", RowBox[{"a", ",", "b"}], "]"}], ShowSpecialCharacters->False, ShowStringCharacters->True, NumberMarks->True], FullForm]\) In[24]:= x /. Or[y__] :> {y} Out[24]= {a || b} Thanks, Istvan
From: Leonid Shifrin on 11 Feb 2010 07:12 Istvan, all you need is to run Trace - it shows pretty clearly what happens: In[3]:= Or[a,b]/.Or[x__]:>{x}//Trace Out[3]= {{{Or[x__],x__},x__:>{x},x__:>{x}},a||b/.x__:>{x},{a||b}} <Or> on your pattern evaluates to the pattern itself before the match is attempted. This is due to the behavior of Or on any single argument - it just returns the argument= .. To avoid this, you have to prevent the evaluation of your rule's l.h.s. Either use HoldPattern: In[4]:= Or[a,b]/.HoldPattern[Or[x__]]:>{x} Out[4]= {a,b} Or Verbatim In[5]:= Or[a,b]/.Verbatim[Or][x__]:>{x} Out[5]= {a,b} or, possibly, wrap the entire rule in Unevaluated: In[8]:= Or[a, b] /. Unevaluated[Or[x__] :> {x}] Out[8]= {a, b} Regards, Leonid 2010/2/11 Istv=E1n Zachar <zac(a)freemail.hu> > Can you please explain why ReplaceAll does not return a simple list? > > In[21]:= ClearAll[x, a, b]; > x = Or[a, b]; > > FullForm@x > > Out[23]//FullForm= \!\(\* > TagBox[ > StyleBox[ > RowBox[{"Or", "[", > RowBox[{"a", ",", "b"}], "]"}], > ShowSpecialCharacters->False, > ShowStringCharacters->True, > NumberMarks->True], > FullForm]\) > > In[24]:= x /. Or[y__] :> {y} > > Out[24]= {a || b} > > > Thanks, > Istvan > >
|
Pages: 1 Prev: radon transform Next: Translating this algorithm into mathematica code |