I am working on the text Programming with Mathematica by Paul Wellin. I some exercise is asked to define the following function using If
in conjunction with Map
or Fold
.
I tried using Fold
without success because of don't know well which expresion in Fold[expr, a, list]
to use. I made my own version using For
as follows
remove3Repetitions[lis_] := Module[{positions = {}, lis2, i = 1},
For[i = 1, i <= Length[lis] - 2, i++,
If[lis[[i]] == lis[[i + 1]] && lis[[i + 1]] == lis[[i + 2]],
positions = Append[positions, {i}];
];
];
lis2 = Delete[lis, positions]
]
This works fine, for example,
In[177]:= remove3Repetitions[{0, 1, 1, 2, 2, 2, 1}]
Out[177]= {0, 1, 1, 2, 2, 1}
But, using the Higher order functions Fold
or Map
may be a better approach. The text ask for this. I would like any suggestion.