Hi Werner,
The ways I can think of immediately are three or four. Since PatternTest
takes as an argument only the whole expression to be tested, if you want to test a relationship between its parts, then you have to get at the parts in some way.
Your first way, with Part
, seems clear except for the mass of punctuation symbols. The [[]]
can be replaced by words, First
and Last
. Since the length of #
is 2, Last[#]
gives the same result as #[[2]]
. This might be appealing to some people:
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(#[[1]] < #[[2]] &) ...}]
(*True *)
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(First[#] < Last[#] &) ...}]
(*True *)
Here is a Less
readable version (hope you don't mind a pun):
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(Less @@ # &) ...}]
(* True *)
I hope the following is not confusing. The named patterns in {x_, y_}
are a separate from the nameless pattern {_, _}
being tested and repeated:
MatchQ[{{1, 2}, {Pi, 3.2}}, {{_, _}?(MatchQ[#, {x_, y_} /; x < y] &) ...}]
(* True *)
This last way is more verbose and probably a bit less efficient. Maybe it's more readable. If so, then unless you are apply such patterns to large amounts of data, the efficiency is probably not as significant a concern as readability. Frankly, it's took me years and years to get comfortable with patterns. As you learn more, what seems readable changes, too.
Anyway, these are the ideas I could come up with. Pick whichever one you find easiest to deal with.