3
$\begingroup$

I want to generate a replacement rule from a user-defined pattern. For example, the user provides a list of patterns {A[i_][j_], BB[k_]} and a user-defined function F, and I hope to generate {A[i_][j_]:>F[A[i][j], B[k_]:>F[B[k]}.

I wonder how to achieve this?

$\endgroup$

1 Answer 1

2
$\begingroup$
Clear["Global`*"]

genRules[patterns_, func_] := 
 Thread[patterns :> 
   Evaluate[(func /@ (patterns /. (x_Pattern :> x[[1]])))]]

genRules[{A[i_][j_], B[k_]}, F]

(* {A[i_][j_] :> F[A[i][j]], B[k_] :> F[B[k]]} *)
$\endgroup$
1
  • $\begingroup$ thanks, works perfectly! $\endgroup$
    – Lelouch
    Mar 7, 2021 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.