This is an answer to your original question:
{{1,2,3},{1,3,2},{3,2,1}}/.z:{a_Integer,b_Integer,c_Integer}/;OrderedQ[z]->1
(* {1, {1, 3, 2}, {3, 2, 1}} *)
Or, since a, b,c are not in your result,
{{1,2,3},{1,3,2},{3,2,1}}/.z:{__Integer}/;OrderedQ[z]->1
(* {1,{1,3,2},{3,2,1}} *)
With respect to the question in your Edit, the first line does not work due to an incorrect positioning of the pattern name t
. As remarked by Henrik Schumacher as well, you should use t:f@@Mapthread[..
instead.
However, then you get a larger output than produced by your second line. Sorting each of the results in this output and then removing the duplicates gives the answer you are looking for:
Attributes[f]=Orderless;
result=ReplaceList[f[1,2,3,4,5],t:f@@(MapThread[Pattern[#1,Repeated[_,{#2}]]&,{{x1,x2,x3},{1,2,2}}])/;(OrderedQ[List/@t])->{{x1},{x2},{x3}}];
DeleteDuplicates [Sort /@ result]
(* {{{1},{2,3},{4,5}},{{1},{2,4},{3,5}},{{1},{2,5},{3,4}},{{2},{1,3},{4,5}},
{{2},{1,4},{3,5}},{{2},{1,5},{3,4}},{{3},{1,2},{4,5}},{{3},{1,4},{2,5}},
{{3},{1,5},{2,4}},{{4},{1,2},{3,5}},{{4},{1,3},{2,5}},{{4},{1,5},{2,3}},
{{5},{1,2},{3,4}},{{5},{1,3},{2,4}},{{5},{1,4},{2,3}}} *)
In your comment on this solution, you asked if this could not be done by a clever use of pattern matching and pattern test only.
After some playing I found the following:
Attributes[f]=Orderless;
ReplaceList[f[1,2,3,4,5],f[x1:Repeated[_,{1}],x2:Repeated[_,{2}],x3:Repeated[_,{2}]]:>{{x1},{x2},{x3}} /; {x2}[[1]]<{x3}[[1]]]
(* {{{1},{2,3},{4,5}},{{1},{2,4},{3,5}},{{1},{2,5},{3,4}},
{{2},{1,3},{4,5}},{{2},{1,4},{3,5}},{{2},{1,5},{3,4}},{{3},{1,2},{4,5}},
{{3},{1,4},{2,5}},{{3},{1,5},{2,4}},{{4},{1,2},{3,5}},{{4},{1,3},{2,5}},
{{4},{1,5},{2,3}},{{5},{1,2},{3,4}},{{5},{1,3},{2,4}},{{5},{1,4},{2,3}}} *)
But this is a very subtle solution. Observe that we do not require that the sequences x2
and x3
are ordered. That was explained by @Martin Ender in his comment on an answer given here. Pattern matching with orderless functions is by no means trivial. If your only reason for using them is to run through all permuations of the arguments, then I would strongly prefer a solution that does not use pattern matching at all, for example:
({#[[{1}]], #[[{2,3}]], #[[{4,5}]]}& /@ Select[Permutations[Range[5]], #[[2]]<#[[3]]&&#[[4]]<#[[5]]&&#[[2]]<#[[4]]&])
(*{{{1},{2,3},{4,5}},{{1},{2,4},{3,5}},{{1},{2,5},{3,4}},
{{2},{1,3},{4,5}},{{2},{1,4},{3,5}},{{2},{1,5},{3,4}},{{3},{1,2},{4,5}},
{{3},{1,4},{2,5}},{{3},{1,5},{2,4}},{{4},{1,2},{3,5}},{{4},{1,3},{2,5}},
{{4},{1,5},{2,3}},{{5},{1,2},{3,4}},{{5},{1,3},{2,4}},{{5},{1,4},{2,3}}} *)