1
$\begingroup$

Is there a way to get the arguments passed to a function? For example, I have the generic function f, then if I have

f[1,2]

I want to get {1,2}, and if I have

f[Sin[x],Cos[x],4,7]

I want to get {Sin[x],Cos[x],4,7}. Thanks!

$\endgroup$
3
  • $\begingroup$ [Sin[x],Cos[x],4,7] is not a valid Mathematica expression. Maybe you can try Sequence @@ f[Sin[x], Cos[x], 4, 7] or f[Sin[x], Cos[x], 4, 7] /. f -> Sequence or List @@ f[Sin[x], Cos[x], 4, 7] or f[Sin[x], Cos[x], 4, 7] /. f -> List? $\endgroup$
    – kglr
    Jul 4, 2017 at 17:11
  • $\begingroup$ I meant the lists. List@@f[...] works fine! Thanks. $\endgroup$ Jul 4, 2017 at 17:15
  • $\begingroup$ At least closely related: Converting a list of rules to a list of lists $\endgroup$
    – Kuba
    Jul 4, 2017 at 20:45

2 Answers 2

2
$\begingroup$
List @@ f[1, 2, 3]

{1, 2, 3}

List @@ f[Sin[x], Cos[x], 4, 7] 

{Sin[x], Cos[x], 4, 7}

or

f[Sin[x], Cos[x], 4, 7] /. f -> List

{Sin[x], Cos[x], 4, 7}

f[1, 2, 3] /. f -> List

{1, 2, 3}

$\endgroup$
0
$\begingroup$

you can also do something like this:

f[arg__][p_] := p @@ {arg}
Through[{f[1, 2], f[Sin[x], Cos[x], 4, 7]}[List]]

(* {{1, 2}, {Sin[x], Cos[x], 4, 7}} *)
$\endgroup$
2
  • $\begingroup$ Thanks! This one works too. $\endgroup$ Jul 6, 2017 at 14:39
  • 1
    $\begingroup$ p@arg is simpler. $\endgroup$
    – kglr
    Jul 6, 2017 at 18:40

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.