# How do I convert an argument list to a list of arguments?

I think this is a basic question, but I am having difficulty finding the answer in the documentation. Thread is not what I am looking for, I think.

Suppose that I have a function f that takes an unspecified number of arguments: f[a, b, c, ...], defined by a declaration like f[lists__] := .... Suppose that I have an argument list {a, b, c, d}. How can I obtain f[a, b, c, d] from {a, b, c, d}? Thanks.

-
f @@ {a, b, c, d}? For more, see the documentation for Apply. –  Oleksandr R. Sep 19 '12 at 20:24
As (by your own admission) this is very basic and well-covered in the documentation, I'm voting to close as too localized. Nothing personal, of course (everyone has mental lapses!), but I doubt this will be terribly useful to the next person. It's up to you of course, but I would tend to suggest asking questions like this informally in the chat. –  Oleksandr R. Sep 19 '12 at 20:28
@OleksandrR. I agree with closing this question. I have added a vote to close. –  Andrew Sep 19 '12 at 20:38
@OleksandrR. I'm hesitating to close this as TL. There are more questions like this on the Internet, so the OP is not alone. And what is obvious to you may not be obvious to everyone. We've had more questions that could be answered by a single link to the documentation and they weren't all closed. So, why close this one? Just curious. –  Sjoerd C. de Vries Sep 19 '12 at 21:06
@Sjoerd I would be in favor of closing all questions that do nothing but duplicate what's already said in the documentation. If I haven't consistently voted as such then either it's an oversight on my part or someone went beyond the documentation in an answer. By the way, I do consider the voting process itself important; as you rightly state, not everyone will take the same view on what is or isn't obvious. If I'm in the minority, I will be happy to see the question remain open. –  Oleksandr R. Sep 19 '12 at 21:18

It seems I have found the answer: Apply.

Apply[f, {a, b, c, d}]


gives the output:

f[a, b, c, d]

The short infix syntax for Apply (at levelspec 0) is @@:

f @@ {a, b, c, d}


f[a, b, c, d]

-

Of course Apply, but also:

 f[{a, b, c, d} /. List -> Sequence]


f[a, b, c, d]

(my finger is hovering above the 'close' link, though)

-
Also, f[Sequence@@{a,b,c,d}] –  2012rcampion Mar 19 at 17:42

A case not covered yet is if the argument list is held.

Starting with a dummy argument list, held:

args = Hold[{2+2, 8/4}];


and a dummy head (function) that also that holds its arguments, for illustration:

SetAttributes[foo, HoldAll]


Here are some options:

foo @@@ args // First

foo @@@ args // ReleaseHold

args /. _[{x___}] :> foo[x]


All yield:

foo[2 + 2, 8/4]


As rcollyer reminds, and I was remiss not to include, Hold can of course have multiple arguments itself therefore a simple Apply can work here:

foo @@ Hold[2+2, 8/4]

foo[2 + 2, 8/4]


Nevertheless sometimes the other format is produced and I hope that my examples prove useful in other circumstances.

-
Hold accepts any number of arguments, so putting them in a nested list is unnecessary. Then, that allows foo @@ args. –  rcollyer Mar 19 at 15:23
@rcollyer I know you know I know this. I guess I should have included that in the answer. Update soon... –  Mr.Wizard Mar 19 at 15:46
I blame laziness; on my part for pointing it out (and not editing it), etc. –  rcollyer Mar 19 at 15:48
@rcollyer lol :-) –  Mr.Wizard Mar 19 at 15:49