Longest¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Examples¶
No verified examples yet for this function.
Implementation notes¶
Longest is a pattern wrapper, not a function. It is recognised structurally by the matcher in match.c: the sequence-matching loop peels off Longest[p] (alongside Pattern, Shortest, and Optional wrappers) before binding, setting an is_longest flag on the underlying pattern p. The flag controls the order in which the backtracking sequence matcher tries argument-count partitions for __/___/Repeated sub-patterns — Longest makes the matcher try the greediest (largest) consumption first (the default for __/___), whereas Shortest flips the order to try the smallest first. It does not change which matches are possible, only which one is found first. Longest appears in the matcher's list of recognised pattern heads that eval.c keeps unevaluated.
Attributes: none registered.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/match.c - Specification:
docs/spec/builtins/pattern-matching.md
Notes & additional examples¶
Worked examples¶
In[1]:= ReplaceList[{a, b, c}, {Longest[x__], y__} :> {{x}, {y}}]
Out[1]= {{{a, b}, {c}}, {{a}, {b, c}}}
In[2]:= MatchQ[{1, 1}, {Longest[1 ..]}]
Out[2]= True
In[1]:= ReplaceList[{1, 2, 3, 4}, {x__, y__} :> {{x}, {y}}]
Out[1]= {{{1}, {2, 3, 4}}, {{1, 2}, {3, 4}}, {{1, 2, 3}, {4}}}
In[2]:= Replace[{a, a, a, b}, {Longest[a ..], rest___} :> {x, rest}]
Out[2]= {x, b}
Notes¶
Longest[p] forces a sequence pattern to consume as many elements as possible. In ReplaceList (Out[1]) the longest partition is tried first; contrast with Shortest, which orders the matches the other way.