The resulting tree of this recursion is not what I what, which probably proves that I don’t fully understand the behaviour of lists/tuples in recursion. If someone could explain what I did wrong in this example and also explain the right way to think I’d be very grateful.
move([],{Main, One, Two}) ->
{Main, One, Two};
move([X|Xr], {Main, One, Two}) ->
[{Main, One, Two}, move(Xr, single(X, {Main, One, Two}))].
Desired result (one list containing 3 tuples):
[{[a,b],[],[]}, {[a],[b],[]}, {[],[b],[a]}, {[b],[],[a]}]
Actual Result(a list containing a tuple and a list, containing a tuple and a list…):
[{[a,b],[],[]},[{[a],[b],[]},[{[],[b],[a]},[{[b],[],[a]}]]]]
You have two problems:
|instead of,as @nmichaels mentioned.move/2returns a list so the terminating clause must also return a list. This is not seen in your example as the first problem hides it.So the resulting code would be:
I flipped the order of the clauses as I personally prefer writing them this way. No fundamental difference in this case. I am assuming that
single/2returns a tuple.You can actually optimise this code by removing all knowledge of the tuple from
move/2as it never actually uses the internal structure. So: