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 theMaxIterations
option. 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.) $\endgroup$MaxIterations
is an option name, not a Symbol with an assigned value, so I do not believe that method will work. You will need to useSetOptions
or provide the option inReplaceRepeated[. . ., options]
. $\endgroup$