The following is just an example that captures what I want to do (more on it at the bottom).
I'm trying to do something that can be paraphrased as follows:
I have a global list, say list = {0,0,1,0,0,1,1,1,1}. I have a function which can modify the global list:
f[element_] := (If[element == 1, list = Select[list, # != 1 &]]; counter++)
My problem is that when I map the function on the list, it goes through all the elements of the initial version of it, without skipping the values that have been eliminated:
counter = 0;
f /@ list;
counter
(* 9 *)
How do I get around this problem? (I'm ok with making the list locally defined)
EDIT: From the comments I gather that it might be useful to explain what I actually want to do. Well, here it is. I have a quantum system which is transformed and it may incur in errors: my list is a list of possible errors. My function looks at the errors and determines if they are correctable or not. To save time, if an error $E$ is not correctable I want to skip those errors in the list that come from composing $E$ with others.
counter...? When you first hit an element that is 1 all the other 1s will not be selected, so what do you need the modified list for? – Marius Ladegård Meyer Feb 09 '16 at 22:03Selectonlistrather than reassigning it lots of times insidef. – evanb Feb 09 '16 at 22:15Selectand leave the original list intact or useSowandReapto build a list of results if you really have to do things iteratively. – Sascha Feb 09 '16 at 22:23/@works as intended: First the right hand side is evaluated and then the function is applied to this (new) List. Changes tolisthave thus no effect on the result. SeeTrace[f/@list]for details. – Berg Feb 09 '16 at 22:33