2

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.

Wjx
  • 9,558
  • 1
  • 34
  • 70
  • "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 06 '16 at 12:31
  • @MacroB Updated~Can this edit explain a bit more? – Wjx Jul 06 '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 06 '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 06 '16 at 22:49
  • As it's already the Shortest one it can find. – Wjx Jul 06 '16 at 22:50
  • I suspect that you want something fundamentally different from the built-in implementation of Longest and Shortest, 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:56
  • @Mr.Wizard er, actually this I ask this question just because I find their priority system is totally seperated and want to know how Mathematica automatically handle the priority between them and how can we manipulate this process. – Wjx Jul 06 '16 at 23:10
  • By my reading however you seem to want the priority parameter to make Longest and Shortest behave 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 of Shortest and Longest do as you illustrated that yourself. – Mr.Wizard Jul 06 '16 at 23:22
  • En, I want to control the priority more, maybe some sort of let it work in a different way. Also I suppose a better understanding will help. – Wjx Jul 06 '16 at 23:35
  • @Mr.Wizard I know the priority settings works pretty well when handling multiple Shortest s or Longest s, 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:40
  • And this problem is just asking for one solution to the thing I've mentioned in the previous comment. The desired result is that when I input Replace[{1, 2}, {Longest[Shortest[x__, 1], 2], y___} :> {x}] I can get Longest work at the top priority thus return {1,2} – Wjx Jul 06 '16 at 23:42
  • Then as I have said several times I think we need a better (and more complex) example where the behavior you wish to see is needed rather than merely incidental. You may have seen the phrase "explain like I'm five." Anyway it's your question and you can update it or not, but without a clarifying update I shall move on to more clear yet interesting questions like (118183). – Mr.Wizard Jul 06 '16 at 23:45
  • Oh, thanks! I'll have to admit that this kind of example is hard to find out. I'll try~ – Wjx Jul 07 '16 at 00:01
  • @Wjx While responding to another one of your questions I think I found an example that you could use to reopen this question. On my system RealDigits[99/700, 10, 24][[1]] /. {pre___, Longest[Repeated[rep__, {2, Infinity}]], inc___} /; MatchQ[{rep}, {inc, __}] :> {{pre}, {rep}, {inc}} gives the answer desired but RealDigits[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:15
  • Sorry, but please forget what I wrote before. I messed with far too many combinations of Shortest and Longest and 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
  • @Mr.Wizard alright……Shortest and Longest can be very very confusing sometimes, especially when using together. And thanks for remembering my problem! – Wjx Aug 01 '16 at 04:42
  • @Wjx Yes they can be. I certainly have not forgotten about it. I have found your series of questions about pattern matching quite interesting. I hope you are not discouraged by this question presently being closed, and I still hope we eventually come up with a clear and concise example and reopen it. – Mr.Wizard Aug 01 '16 at 04:47
  • @Mr.Wizard wow, a thousand thanks for your appreciation to this series. I am constantly thinking about examples as well, but as you can see, a good example is hard to find...... – Wjx Aug 01 '16 at 04:50

0 Answers0