Martin provided a nice answer with SequenceCases
. For Mathematica versions prior to 10.1, depending on what OP wants for lists of the form:
list2 = {1, 2, 4, 5, 7, 11, 8, 9, 10, 12, -3, -2, 6, 7, 80};
namely, for lists that have consecutive sequences of same monotonicity, an approach could be:
splitMon[list_] :=
Split[list,
sign =.;
If[sign (#2 - #1) > 0, True, sign =.; False, sign = Sign[#2 - #1]; True] &
];
Examples:
SequenceCases[list, x_ /; Less @@ x || Greater @@ x]
% === splitMon[list]
(* {{1, 2, 4, 5, 7, 11}, {8, 7, 3, 1, -3}, {-2, 6, 7, 80}} *)
(* True *)
SequenceCases[list2, x_ /; Less @@ x || Greater @@ x]
% === splitMon[list2]
(* {{1, 2, 4, 5, 7, 11}, {8, 9, 10, 12}, {-3, -2, 6, 7, 80}} *)
(* True *)
{1, 2, 3, 1, 2, 3}
? Will you rely on sequences alternating between increasing and decreasing (which would probably yield{{1, 2, 3}, {1}, {2, 3}}
) or do you want to just pick monotonic sequences greedily (resulting in{{1, 2, 3}, {1, 2, 3}}
)? $\endgroup$