For example, I have defined a bunch of binary functions Sum1
, Sum2
, Prod1
, Prod2
, ... I want to make these functions associative, that is, the expression Sum1[a,b,c]
will be evaluated as Sum1[Sum1[a,b],c]
. I use the method here to achieve it:
Sum1[a_,b_,c__]:=Sum1[Sum1[a,b],c]
Now I want to do this to all of these binary functions without repeating it line by line (because there are too many of them). Is there a way to do it? I tried to do the following
Do[f[a_,b_,c__]:=f[f[a,b],c],{f,{Sum1,Sum2,Prod1,Prod2}}]
But after this, when I evaluate Sum1[a,b,c]
, it will return f[f[a,b],c]
! In other words, the f
after the :=
is not substituted by the instances from the list {Sum1,Sum2,Prod1,Prod2}
.
Do[With[{f=f}, f[a_,b_,c__]:=f[f[a,b],c]],{f,{Sum1,Sum2,Prod1,Prod2}}]
$\endgroup$With[{f = f}, ...]
you really should be using aFunction
and some mapping construct instead. $\endgroup$