I wish to create a function that takes lists (e.g. {plus,plus,minus,0,minus,plus}
) and uses the input list to calculate a corresponding value using a set of recursive rules.
I want the function to apply the following algorithm:
1) Read the first element of the list
2) Calculate the numbers of 'plus' in the rest of the list (i.e. the list not including the first element).
3) Based on whether the answer to part 2 is odd or even, a starting value (==1 for simplicity
) is multiplied by a value, and the first element of the input list is removed.
4) Steps 1-3 are applied to the list until there is only one element left in the list. (The value of the list with one element is already known.
To take an example: Say I want to calculate the value for the list {plus,plus,minus}. It is already known that f[{minus}] = $\alpha_2$ (I define this previously) We have:
- f[plus,{...}] = ($\alpha_1-\beta_1$)*f[{...}] (even # of plus in {..})
- f[plus,{...}] = $\alpha_2$*f[{...}] (odd # of plus in {..})
- f[minus,{...}] = $\alpha_2$*f[{...}] (even # of plus in {..})
- f[minus,{...}] = $\alpha_1-\beta_1$*f[{...}] (odd # of plus in {..})
(There are also rules for f[{0,...}]
but I have left them out for simplicity as they are not relevant to this example.)
f[{plus,plus,minus}] = $\alpha_2$*f[{plus,minus}] = $\alpha_2*(\alpha_1-\beta_1)$f[{minus}] = $\alpha_2^2*(\alpha_1-\beta_1)$
I would like to be able to apply this list to lists of arbitrary length (and also incorporate more rules if necessary.
I am a novice in Mathematica, and have tried using recursive functions as described in the documentation, but I am unfamiliar as to what sort of syntax I would need to use to manipulate the lists in the way I want. I have also considered using For loops to automate this process but I am aware this would make the process very inefficient, and this is also an issue as I would need to run the function many, many times.