I'm using the following code to pick out (match) two specified items in a list (specified as anum_
and bnum_
), change them both and then return an output list. The output list has the two specified items changed but the rest left intact:
qubits = {qubit[1, "x", "y", "z"], qubit[2, "x", "y", "z"], qubit[3, "x", "y", "z"]};
processQubits[anum_, bnum_, {before___, qubit[anum_, s1_, s2_, s3_], middle___, qubit[bnum_, t1_, t2_, t3_], after___}] := {before, qubit[anum, s1<>t1, s2<>t2, s3<>t3], middle, qubit[bnum, s1<>t1, s2<>t2, s3<>t3], after}
(*Matches and returns "xx", "yy", "zz" etc.*)
processQubits[1, 2, qubits]
(*Does not match due to ordering*)
processQubits[2, 1, qubits]
Out[4]= {qubit[1,xx,yy,zz],qubit[2,xx,yy,zz],qubit[3,x,y,z]}
Out[6]= processQubits[2,1,{qubit[1,x,y,z],qubit[2,x,y,z],qubit[3,x,y,z]}]
Unsurprisingly, the match succeeds when I list the items in the order in which they appear in the list (e.g. processQubits[1, 2, ..]
, but not when I list them in reverse order (e.g. processQubits[2, 1, ...]
).
Is there a well-used idiom to match these two items and return an output list regardless of which order the items are specified in?