Timeline for Is there a difference between Replace with parameter "All" and ReplaceAll
Current License: CC BY-SA 4.0
18 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 2, 2019 at 13:00 | history | edited | Lukas Lang | CC BY-SA 4.0 |
added 132 characters in body
|
Jan 1, 2019 at 20:22 | comment | added | Ralph Dratman | Can anyone suggest a broad-strokes overview explanation as to why these particular policies determining where a replacement is or is not applied have been adopted for Replace and ReplaceAll? | |
Dec 31, 2018 at 0:54 | vote | accept | StarBucK | ||
Dec 31, 2018 at 0:54 | comment | added | StarBucK | Your code was extremly usefull | |
Dec 31, 2018 at 0:52 | comment | added | StarBucK | Allright so in summary : ReplaceAll indeed goes from the top to the bottom and as soon as it finds something working, it stop to study further the branch of the tree (the bottom levels of the part where the pattern matched). ReplaceAll always include the Heads in its study. However the replace with parameter All goes from bottom to top and study first the lowest level. But if it finds on a given Part a pattern that matches, it will still continue to study the above level. This is the main difference you explained right ? (and we have to tell him if we also include the heads) | |
Dec 31, 2018 at 0:01 | comment | added | StarBucK | Wow thanks a lot, I will look carefully at all this. And thanks for the distinction level/part, it is actually very usefull as im still a little confused by this | |
Dec 30, 2018 at 22:57 | comment | added | Lukas Lang |
If you still have doubts, you can try to play around with the ReplaceAllVerbose /ReplaceVerbose functions above for other examples, to see in what order parts are visited and what happens to them. They do not support list of rules, but otherwise they should be fully equivalent to the built-in versions in terms of results and functionality.
|
|
Dec 30, 2018 at 22:55 | history | edited | Lukas Lang | CC BY-SA 4.0 |
added 1693 characters in body
|
Dec 30, 2018 at 22:43 | comment | added | Lukas Lang |
Updated the answer again - you simply forgot to consider the whole expression before any of its parts in your attempted explanation above. The level 0 has nothing to do with part 0 : Mathematica distinguishes between part and level specifications. E.g. in f[g[x],y] , part 0 is f , 1 is g[x] , 2 is y , {1,0} is g and {1,1} is x . Level 0 on the other hand is the whole expression f[g[x],y] , level 1 is the list of expressions that are parts of level 0, so {f,g[x],y} (assuming heads are included). Level 2 is similarly given by {g,x} . See also Level[f[g[x],y],{1},Heads->True]
|
|
Dec 30, 2018 at 22:37 | history | edited | Lukas Lang | CC BY-SA 4.0 |
added 2014 characters in body
|
Dec 30, 2018 at 22:11 | comment | added | StarBucK | Sorry I still have a problem, I edited. | |
Dec 30, 2018 at 19:45 | comment | added | Lukas Lang |
Yes - you can also consider ReplaceAll[{f,f},f->g] . Clearly, both f should be replaced, so both parts 1 and 2 of {f,f} . Now consider ReplaceAll[f[f,f],f->g] - this is exactly the same, only that now parts 0,1 and 2 of f[f,f] need to be replaced.
|
|
Dec 30, 2018 at 19:35 | comment | added | StarBucK | I think I see what you mean. When it says "no further rules are tried on that part or any of its subpart", we talk precisely about the part of the element that has been replaced. So here I replaced f which doesn't have part. Thus it stops. We don't talk about the next part of the whole expression that would be f[f[x]][[1]] in this example. (We first replaced f[f[x]][[0]], but the next part is not understood as f[f[x]][[1]] but as the next part of the element that has been replaced which is a part of f, which doesn't exist). Am I correct ? | |
Dec 30, 2018 at 19:12 | comment | added | Lukas Lang | Does the update answer your questions? | |
Dec 30, 2018 at 19:12 | history | edited | Lukas Lang | CC BY-SA 4.0 |
added 1542 characters in body
|
Dec 30, 2018 at 18:48 | comment | added | StarBucK | I still have a problem actually, I made an edit :) Thanks for your answer (things start to be more clear but not totally) | |
Dec 30, 2018 at 18:10 | vote | accept | StarBucK | ||
Dec 30, 2018 at 18:48 | |||||
Dec 30, 2018 at 17:04 | history | answered | Lukas Lang | CC BY-SA 4.0 |