问题
I'd like to extract arguments from instances of Inequality. Following doesn't work, any idea why and how to fix it?
Inequality[1, Less, x, Less, 2] /. Inequality[a_, _, c_, _, e_] -> {a, c, e}
回答1:
Inequality[1,Less,x,Less,2] /. HoldPattern[Inequality[a_,_,b_,_,c_]] -> {a, b, c}
Out: {1, x, 2}
回答2:
Also, you can do this:
Inequality[1, Less, x, Less, 2] /. Literal @ Inequality[ a_ , _ , c_ , _ , e_ ] -> {a, c, e}
ADL
回答3:
Why don't you use standard access to subexpression?
expr = Inequality[1, Less, x, Less, 2]; {a,c,e} = {expr[[1]], expr[[3]], expr[[5]]};
来源:https://stackoverflow.com/questions/3851390/pattern-matching-inequality