I need to improve a pattern or switch an approach.
It is best described by an example
For a hierarchy/order given by a list e.g.:
order = {1, 2, 3}
and a list:
list = {
1, 2, 3, 2, 3, 3, 2, 3, 3, 2, 3, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3,
3, 3, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3, 3, 3
}
I need to verify that list
matches a sequence defined by order
:
MatchQ[list, {PatternSequence[1, PatternSequence[2, 3 ..] ..] ..}]
This pattern scales very poorly, already that one won't finish evaluating.
The function should only take list
as an argument, consider the order constant. The pattern does not need to be constructed automatically.
Repeated
can lead to immense back-tracking in the evaluation. I assume that is what's happening in this example. Maybe a finite state machine approach would work better? $\endgroup$