Putting things inside HoldPattern

I am creating a list of replacement rules like this

ops = {LessEqual, GreaterEqual};
ineqRules = Table [HoldPattern [Inequality [a_, op1, c_, _, e_]] -> a == c, {op1, ops}]

Above does not work, because "op1" is hidden from the HoldPattern table, how to fix it?

This is a continuation of the previous question.

+3
source share
3 answers

What about

ops = {LessEqual, GreaterEqual};    
ineqRules = (HoldPattern[Inequality[a_, #, c_, _, e_]] :> a == c) & /@ ops

Edit: To fix the problem noted in the lumbar answer, try:

ineqRules=Flatten[{HoldPattern[Inequality[a_,#,c_,___]]:>a==c,HoldPattern[#[a_,c_]&&___]:>a==c}&/@ops]

This obviously depends on what you start with a simple structure, i.e. no other && 's.

+2
source

This is a job for With:

ops = {LessEqual, GreaterEqual};
ineqRules =
  Table[
    With[{op1=op1},
      HoldPattern[Inequality[a_, op1, c_ ,_ ,e_]] -> a == c
    ],
    {op1, ops}
  ]
+2
source

, , :

ops = {LessEqual, GreaterEqual};
ineqRules[op_] := HoldPattern[Inequality[a_, op, c_, _, e_]] -> a == c;
ineq = Table[ineqRules[op], {op, ops}];
Inequality[1, LessEqual, x, Less, 2] /. ineq

Out: 1 == x

HTH

:

Inequality[e1, GreaterEqual, e2, Equal, e3] /. ineq
Out> e1 == e2

Inequality[1, GreaterEqual, e2, Equal, 2] /. ineq
Out> False

, - Hold [], , ...

+1
source

Source: https://habr.com/ru/post/1767798/


All Articles