As is shown in this post's answer, it seems that though you can set priority of Longest (or Shortest) by Longest[patt,prio], There's no direct way (at least no direct way as far as I know) to set priority between them.
It means that the priority setting with Shortest will just define the priority between multiple Shortests but leave Longest uninfluenced. But I suppose there must be some cases when we need to specify the priority between them, for example, we need pattern1 to be shortest, after that, we need to make pattern2 longest, then we need to make pattern3 shortest.
How can we do this? Any help will be appreciated, thanks!!!
I'll update if I can find any proper example of this.
Update 1
I'll have to admit though I think this problem as quite prevailing, it's quite hard to find an example in a few days. But here is one:
Replace[{1, 2}, {Longest[Shortest[x__]], y___} :> {x}]
({1})
Replace[{1, 2}, {Shortest[Longest[x__]], y___} :> {x}]
({1,2})
It seems that when Longest and Shortest compete with each other in this simple case, it will put inner level at a higher priority. But if I want to use the first code while getting the second result, the most natural way is to add a priority to it. However, the following code will not work.
Replace[{1, 2}, {Longest[Shortest[x__, 1], 2], y___} :> {x}]
({1})
Just as I've said before, Longest and Shortest have their own priority, so this method will not work.
When meeting with similar question when two Longest or two Shortest compete with each other, we can usually solve them by adding a priority:
Replace[{1, 2, 3, 4}, {Longest[x__, 1], Longest[y__, 2]} :> {{x}, {y}}]
I'm just wondering how can we do the same when there's conflict between Longest and Shortest.
x__be simultaneously Shortest and Longest? – Mr.Wizard Jul 06 '16 at 22:37Longestis similar, as you cannot let two sequence simultaneously Longest, but in this case the problem can be solved. I supposeLongestandShortestshouldn't be considered as actually longest or shortest, but the longest and shortest it can get within the restrictions. So if aLongestwith higher priority already set a limit to it, it must follow. Thus, if we can define the priority in my case, for example, setLongestat a higher priority, theShortestshall find there's no other option but one, thus obey the result given byLongest. – Wjx Jul 06 '16 at 22:49Shortestone it can find. – Wjx Jul 06 '16 at 22:50LongestandShortest, even with priority specified. I feel that you still need to provide a better and more complete example that does not have a trivial reduction. – Mr.Wizard Jul 06 '16 at 22:56LongestandShortestbehave differently from the way it does now. Your question as written does not appear to be about understanding the existing behavior but rather extending it in a particular way, i.e. "I suppose there must be some cases when we need to specify the priority between them ..." I think you already understand what the priority parameter ofShortestandLongestdo as you illustrated that yourself. – Mr.Wizard Jul 06 '16 at 23:22Shortests orLongests, but it's simply wierd that we cannot set priority between them, it doesn't seem like a hard problem to set priority between them as they are the same sort of function and if we do so, all the actions we can do now can be done in the same way, but we can get a much better control of the priority. – Wjx Jul 06 '16 at 23:40Replace[{1, 2}, {Longest[Shortest[x__, 1], 2], y___} :> {x}]I can getLongestwork at the top priority thus return{1,2}– Wjx Jul 06 '16 at 23:42RealDigits[99/700, 10, 24][[1]] /. {pre___, Longest[Repeated[rep__, {2, Infinity}]], inc___} /; MatchQ[{rep}, {inc, __}] :> {{pre}, {rep}, {inc}}gives the answer desired butRealDigits[99/700, 10, 24][[1]] /. {pre___, Longest[Repeated[Shortest[rep__], {2, Infinity}]], inc___} /; MatchQ[{rep}, {inc, __}] :> {{pre}, {rep}, {inc}}gives the "wrong" one. (continued) – Mr.Wizard Jul 31 '16 at 10:15ShortestandLongestand rather confused myself. I updated my answer to the linked question accordingly. I don't think this can be used as a good example for this question. – Mr.Wizard Jul 31 '16 at 13:30