I'd like to construct a function, that changes its own input. The code
SetAttributes[f, HoldFirst]; l={1,2,3};
f[l_List] := Module[{i}, For[i=1, i<=Length@l, i++, Print[i]; l[[i]]=1;]];
f[l]; l
returns {1,2,3} instead of {1,1,1}. What am I doing wrong?
f[l_]:=..., then it works. :/ – Leo Aug 12 '18 at 21:44HoldFirstattribute means that theSymbolis passed, not an expression with a headList. – Michael E2 Aug 12 '18 at 21:45_List? – Leo Aug 12 '18 at 21:47Do[l[[i]] = 1, {i, Length@l}]seems like better code for the body off. (I realize it's just a test example, butForis rarely used by Mathematica programmers.) – Michael E2 Aug 12 '18 at 21:47l_?ListQwhich will test without theHold– b3m2a1 Aug 12 '18 at 22:08