# How to Set Priority Between Longest and Shortest [closed]

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.

## closed as unclear what you're asking by MarcoB, Bob Hanlon, Öskå, Jens, Mr.Wizard♦Jul 11 '16 at 0:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• "I suppose there must be some cases when we need to specify need to priority between them": and yet, you can't even come up with an example for such a need? Aren't you perhaps trying to solve a non-existent problem? Unfortunately the question seems rather unclear to me as written. – MarcoB Jul 6 '16 at 12:31
• @MacroB Updated~Can this edit explain a bit more? – Wjx Jul 6 '16 at 15:17
• Your example does not make sense to me. How can a single pattern x__ be simultaneously Shortest and Longest? – Mr.Wizard Jul 6 '16 at 22:37
• @Mr.Wizard I suppose the example with two Longest is similar, as you cannot let two sequence simultaneously Longest, but in this case the problem can be solved. I suppose Longest and Shortest shouldn't be considered as actually longest or shortest, but the longest and shortest it can get within the restrictions. So if a Longest with higher priority already set a limit to it, it must follow. Thus, if we can define the priority in my case, for example, set Longest at a higher priority, the Shortest shall find there's no other option but one, thus obey the result given by Longest. – Wjx Jul 6 '16 at 22:49
• As it's already the Shortest one it can find. – Wjx Jul 6 '16 at 22:50