Here is my code.
{2, 9, 3, 16, 81, 0.09} //. {x_Integer -> Sqrt[x]}
The pop-up message ReplaceRepeated::rrlim suggests that this rule can be applied an infinite number of times.
I am supposed to get {Sqrt[2],Sqrt[3],Sqrt[3],Sqrt[2],Sqrt[3],0.09}
What's wrong with my code here?
Sqrt[2]) contents? – ciao Feb 29 '20 at 10:16{y : Power[_, _] -> y, x_Integer -> Sqrt[x]}for the rules. More importantly, do you see why the example evaluates infinitely? – ciao Feb 29 '20 at 10:20x_Integerstill assumes Sqrt[2] and Sqrt[3] are Integer. – kile Feb 29 '20 at 10:262inSqrt[2], does it not? So that2is transformed toSqrt[2], which is then "inside" the originalSqrt, leading toSqrt[Sqrt[2]].ReplaceRepeatedis effectively the fixed point ofReplaceAll, which will drill into expressions until a pattern match is (or is not) found.The result you are getting is exactly what you should be getting. You need to provide the pattern to match the parts to be left alone, as in my comment example.
A review of the documentation for the various
– ciao Feb 29 '20 at 10:29Replacespecies will be helpful here.{2, 9, 3, 16, 81, 0.09} /. x_Integer -> Sqrt[x]– OkkesDulgerci Feb 29 '20 at 12:349to3, not toSqrt[3]as desired. – Michael E2 Feb 29 '20 at 14:36{2, 9, 3, 16, 81, 0.09} //. {x___, y_Integer, z___} :> {x, Sqrt[y], z}is another way to replace only at the first level. – LouisB Mar 01 '20 at 21:04