I can define conditions on individual arguments to a function:
f[a_List/;condition(a),b_List/;condition(b)]:=(...)
I am wondering if there is a way of declaring something like this:
f[a_List,b_List/;condition(a,b)]:=(...)
so for instance, I might want to be sure that a and b are of the same size or that {a,b} follows a certain pattern for example.
I know I can leave the condition cases inside the body of the function but I am wondering if there is any way of avoiding that.
patt /; test
is a shorthand forCondition[patt, test]
. You are writingf[a_, Condition[b_, test[a,b]]]
, but there is noa
in the pattern in the first argument ofCondition
in this case. Note also that function definitions in Mathematica are nothing but patterns, so it is harmless to put the entire left hand side in the first argument ofCondition
, then you will have access to all symbols in the second argument ofCondition
. This is what eldo does. $\endgroup$