I have a list of functional rules of the type F[x_]:=y;cond[x] with always the same condition. The list is long and rule 1 of programming says: "Do not copy!" I surely can somehow "extract" my condition?!

fromherecondmusthold statement
F[x_]:=y; (*if cond[x]*)
G[x_]:=z; (*if cond[x]*)
fromherecondmustnolongerhold statement

As you see, a slight snag would be that F[u_]:=v would not match the pattern, although an underscored variable is a bound one that could have any other name.


1 Answer 1


As an example we choose a condition x>0:

ClearAll[f1, f2];
With[{y = x_ /; x > 0},
 f1[y] = x;
 f2[y] = 2 x;

Now to test:

  • $\begingroup$ I desperately tried to include 2 variables. Say, f computes y-x but only if y>x. I tried out about any syntax...except the correct :-/ Can you help again? $\endgroup$ Nov 2, 2021 at 10:45
  • $\begingroup$ Try: Clear[f1, cond]; cond[x_, y_] := (x > y); f1[x_, y_] := x + y /; cond[x, y]; $\endgroup$ Nov 2, 2021 at 11:24
  • $\begingroup$ That works for one function, but not for many. I tried the following: With[{assigns}/;cond,f1=...;f2=...;] is wrong syntax, With wants a list. Even a nested With doesn't scope where it should: With[{v = b_}, With[{u = a_ /; u > v}, g[u, v] := a/b]];. It also doesn't matter if u>v is a>b or any mix - the outer assign doesn't make it into the inner bracket. $\endgroup$ Nov 3, 2021 at 9:35
  • $\begingroup$ No, the idea was: Clear[f1, cond]; cond[x_, y_] := (x > y); f1[x_, y_] := x + y /; cond[x, y];f2[x_, y_] := x +2 y /; cond[x, y];... repeating cond[...] $\endgroup$ Nov 3, 2021 at 9:55

Your Answer

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

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