This works:
In[1]:= testList = {1, 2, 3};
In[2]:= testList[[2]] += 7;
In[3]:= testList
Out[3]= {1, 9, 3}
I want to generalize it like this (this is not what Mathematica actually does, but it shows the results I want to accomplish somehow):
In[4]:= add7To2ndElement[list_] := list[[2]] += 7
In[5]:= Module[
{list = {1, 2, 3}},
add7To2ndElement[list];
list
]
Out[5]= {1, 9, 3}
This is what Mathematica actually does, instead of what I'm trying to accomplish:
In[4]:= add7To2ndElement[list_] := list[[2]] += 7
In[5]:= Module[
{list = {1, 2, 3}},
add7To2ndElement[list];
list
]
During evaluation of In[5]:= Set::setps: {1,2,3} in the part assignment is not a symbol.
Out[5]= {1, 2, 3}
How can I rewrite my code so that it alters the list-element in place? The actual list I'm working on is large, and I have to modify its elements a large number of times, so for efficiency's sake, I need to avoid methods that make a fully copy of the list each time I modify an element.