1
$\begingroup$

How can I create a list of variables of simple functions with arguments? These functions will be defined by the user, to be manipulated with some code later.

f1[x1_,x2_,x3_]:=x1*x2-x3;
f2[y1_,y2_]:=y1/y2;
f3=[z1_,z2_,z3_,z4_]:=z1+z2+z3-z4;

I want the lists to be:

(* {x1, x2, x3} *)
(* {y1, y2} *)
(* {z1, z2, z3, z4} *)
$\endgroup$
  • $\begingroup$ There could be a lot of bullet-proofing needed here. What if the user "accidentally" defines f1[x1_, x2_] := x1 * x2 in addition to the three-argument definition you give? What response would you want? $\endgroup$ – Michael E2 Sep 30 '15 at 12:42
  • $\begingroup$ I agree. Actually, the goal is just do a function to create a list of variables, so it is not necessary to do it by hand. I (user) define the functions and MMA create the lists... of course, junk in, junk out .... $\endgroup$ – LeoRon7 Sep 30 '15 at 13:13
2
$\begingroup$
In[4]:= ReleaseHold[List @@@ # & /@ Part[DownValues /@ {f1, f2, f3}, All, 1, 1, {1}]] /. Pattern -> (# &)

Out[4]= {{x1, x2, x3}, {y1, y2}, {z1, z2, z3, z4}}
$\endgroup$

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.