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?


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]]} *)
  • $\begingroup$ thanks, works perfectly! $\endgroup$
    – Lelouch
    Mar 7 '21 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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