Ok, this will probably be a silly question, but I can't get my head around it. I'm making a simple module that, given a list and two integers, will swap the position of the elements in the list.(probably there's a function that does it, but I'm interested in doing it myself) This is what I have so far:
Exchange[list_, i_, j_] := Module[{temp},
temp = list[[i]];
list[[i]] = list[[j]];
list[[j]] = temp;
Return[list];
]
Then I do: list1 = {1, 2, 3, 4}
And finally:
Exchange[list1, 1, 2]
But it returns this error: Set::setps: "{1,2,3,4} in the part assignment is not a symbol."
And the list is unchanged. I looked at the help but I don't understand how I'm supposed to make a valid assignment. I know that what is wrong are the lines 2 and 3 of the module...
Any help is greatly appreciated
list1[[{1, 2}]] = list1[[{2, 1}]]; list1
$\endgroup$list1
? Because then, non of the answer will help you (except the first block of kgugler which doesn't use a Module). $\endgroup$