I am going to test your patience as alas my problem is not quite solved.
I am doing list manipulation in a function that has a list passed to it from another function. So probably my main problem is one of passing by reference or value or not understanding how to manipulate variables. Apologies again if this in an insanely stupid question
A simplified version of the code
UpperChain = {-10, 1, 2, 3, 4}
LowerChain = {-10, 1, 2, 3, 4}
SpinResult = 7
If[ SpinResult < 19,
LowerChain = Win[LowerChain]; UpperChain = Lose[UpperChain];,
UpperChain = Win[UpperChain]; LowerChain = Lose[LowerChain];
];
Win[Chain_] := (
Bet = Chain[[2]] + Last[Chain];
(*
Next line generates an error "is not a variable with a value, so
its value cannot be changed." I was hoping to end up with Chain={-10,1,2,3,4,5}
*)
AppendTo[Chain, Bet];
(*
If I use Chain=Append[Chain,
Bet] this generates an error about the "shape" of the variable
*)
Chain)
Lose[Chain_] := (
(*
Delete element 2 and the last element,
so that I hope to end up with chain = {-10,2,3}
*)
Chain = Delete[Chain, 2];
Chain = Delete[Length[Chain]];
Chain)
Hopefully you can see is that the functions "win" and "lose" simply take a parameter of a "chain" list and then attempt to add a member or delete a member.
So basically my question is, what is the best way to pass lists to functions ( or perhaps one might consider them procedures ) where they are manipulated, and the original list is then modified to reflect the result of the function's manipulation, including increasing and decreasing the length of the list. In C++ one would use linked lists and pointers, I was hoping to use the list manipulation code in Mathematica and pass the list as a parameter by reference and I understand with the use of HoldAll one can do something similar, but alas the HoldAll documentation doesn't really explain such an application.