# Why does using replacement rules give a different result than setting the variable? [duplicate]

These two methods should give the same result, but one terminates when the result is unchanged (to machine precision) while the other continues for the full 100 allowed cycles. Why?

x = -.01;
FixedPointList[(#^2 + x) &, x, 100]

(*  {-0.01, -0.0099, -0.00990199, -0.00990195, -0.00990195, -0.00990195,
-0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195}  *)

FixedPointList[(#^2 + y) &, y, 100] /. y -> -.01

(* {-0.01, -0.0099, -0.00990199, -0.00990195, -0.00990195, -0.00990195,
-0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195,
-0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195,
...
-0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195,
-0.00990195, -0.00990195, -0.00990195, -0.00990195, -0.00990195}*)


## marked as duplicate by Mr.Wizard♦Feb 19 '16 at 5:30

• No they should not. The order of evaluation is not the same. In your second example, the specific numerical value for y gets substituted only at the very end, and this leads to FixedPointList being evaluated completely differently. I described a similar issue here. – Leonid Shifrin Feb 18 '16 at 22:49
• This is a bit like asking why putting your socks on after your shoes looks different from the usual. – J. M. is away Feb 19 '16 at 4:00
• This question did not arise from a syntax, capitalization, spelling, or typographical error. The answer is only easy to find in the documentation if you already know the answer is "order of operations" and search on that. – Jerry Guern Feb 19 '16 at 4:37
• This is a reasonable even if beginner-ish question. Others will have this question, and @Edmund gave a great succinct explanation in his answer below that they'll find helpful. – Jerry Guern Feb 19 '16 at 4:37

Have a look at the Outline of operators in order of decreasing precedence table in The Syntax of the Wolfram Language guide. There you will see that Pattern and rule operators are much further down the list than Function application variants. Therefore, FixedPointList is evaluated before ReplaceAll (/.) which leads to the 100 entries. Consider the steps that follow.

Run the following:

FixedPointList[(#^2 + y) &, y, 15]


This evaluates to the list of 15 iterations of the function such that

FixedPointList[(#^2 + y) &, y, 15] /. y -> -.01


is replacing the ys in the list.

Now run the following:

exp = Hold[FixedPointList][(#^2 + y) &, y, 15] /. y -> -.01


Here FixedPointList is stopped from executing before the replacement such that

ReleaseHold@exp


runs the FixedPointList with y replaced.

Also with Inactive and Activate.

exp = Inactive[FixedPointList][(#^2 + y) &, y, 15] /. y -> -.01


then

Activate@exp


Hope this helps.

• Thank you! I never actually understood what the heck anyone would use Hold[] for, now I see I need it. – Jerry Guern Feb 19 '16 at 4:41
• @JerryGuern Unevaluated is usually a cleaner way to handle this case, e.g. (30193) as well as the question now linked in the "already has an answer" header above yours. – Mr.Wizard Feb 19 '16 at 5:34
• @Mr.Wizard Yes, I see your point that the other question has the same answer as mine, but I don't think that makes the questions "duplicates". A non-expert with MY question would not realize (based on the title) that 30193 has his answer. I'm not just defending my precious question here, I'm trying to keep this site accessible to bewildered noobs. – Jerry Guern Feb 19 '16 at 5:46
• @Jerry This is actually how the "duplicate" system is supposed to work; your question provides a way for people to find (through searching, etc.) a question similar to their own, then it links them to an "original" with multiple hopefully-useful answers. "Duplicate" doesn't mean "stupid noob, search harder next time" but rather "here is another question that was hard to find that has answers in common; now your question will make it easier to find for everybody else." :-) – Mr.Wizard Feb 19 '16 at 6:11
• @Mr.Wizard Hm. Ok, well thank you for that explanation. – Jerry Guern Feb 19 '16 at 6:53