Pattern matching is great but it has its limitations. Consider sorting a list of numbers from smallest to largest:
RandomSample[Range[10]] //. {a___, b_, c_, d___} /; b > c :> {a, c, b, d}
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
However, running
RandomSample[Range[1000]] //. {a___, b_, c_, d___} /; b > c :> {a, c, b, d}
ReplaceRepeated::rrlim: Exiting after {<<1000>>} scaned 65536 times. >>
{1, 2, 3, 4, 5, 6, 7, 9, 11, 13, <<990>>}
is a different story. The code takes a long time to run and the resulting output is not sorted properly.
Now, obviously, we would want to use Sort in this type of scenario, but now I'm curious to know: at what point does a job become too computationally intensive for the pattern matcher?


ReplaceRepeated::rrlim), which you don't mention in your question, notes that this is a limitation ofReplaceRepeated, rather than the pattern matcher. You can change the threshold using theMaxIterationsoption. As far as I know, if you use pure pattern matching, there is no in-principle limitation except that dictated by the scaling behavior of the pattern you are matching. (In this case, the asymptotics are very poor, so that it takes a lot longer on a longer list is no surprise.) – Oleksandr R. Jun 02 '13 at 04:52MaxIterationsis an option name, not a Symbol with an assigned value, so I do not believe that method will work. You will need to useSetOptionsor provide the option inReplaceRepeated[. . ., options]. – Mr.Wizard Jun 02 '13 at 04:57ReplaceRepeated, rather than a global value like$IterationLimit. You have to write theReplaceRepeatedcall out in full rather than using its sigil (//.). So, for example:ReplaceRepeated[RandomSample[Range[1000]], {a___, b_, c_, d___} /; b > c :> {a, c, b, d}, MaxIterations -> Infinity]. But, in general, one should beware of patterns such as this, because pathological scaling is all too easy to encounter. – Oleksandr R. Jun 02 '13 at 04:59