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?


1 Answer 1


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
    Commented 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.