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

30

4

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.

Andrew

Posted 2012-09-19T20:22:30.267

Reputation: 9 123

Related, possible duplicate: (4512)

– Mr.Wizard – 2015-08-03T02:09:11.030

8f @@ {a, b, c, d}? For more, see the documentation for Apply. – Oleksandr R. – 2012-09-19T20:24:53.200

2As (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. – 2012-09-19T20:28:12.620

1@OleksandrR. I agree with closing this question. I have added a vote to close. – Andrew – 2012-09-19T20:38:37.663

Possible duplicate: http://stackoverflow.com/questions/5746717/apply-list-to-arguments-in-mathematica

– Sjoerd C. de Vries – 2012-09-19T20:58:40.580

2@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 – 2012-09-19T21:06:11.850

If this gets closed, it should be as a duplicate of some other question (on this site.) I haven't found one yet, which I'm sure is a weakness in my searching skills. I agree with Sjoerd that 'too localized' isn't quite the correct reason. – Brett Champion – 2012-09-19T21:08:04.767

1@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. – 2012-09-19T21:18:13.270

David reviewed Apply here. The answer matches, but the question doesn't, so I'm not sure that it can be considered a duplicate as such even though the content is already here on the site.

– Oleksandr R. – 2012-09-19T21:39:26.587

@OleksandrR. A list of recent easy questions in chat. Really close those too?

– Sjoerd C. de Vries – 2012-09-19T22:12:23.270

@SjoerdC.deVries Here's a dupe on this site: http://mathematica.stackexchange.com/q/5432/5

– rm -rf – 2012-09-20T13:46:32.730

@r.m close, but not perfect. That question was about prefix. – Sjoerd C. de Vries – 2012-09-20T17:28:40.173

@SjoerdC.deVries Don't just go by the title of the question... I feel that the spirit of the question was about having a list of arguments and being able to pass that list as multiple arguments to a function. It seemed to me that they had no clue what they were asking about and thought that @@ and @@@ are just different forms of prefixes stringed together. I'm also OK with not closing as a dupe of that... just pointed it out since Brett wanted a question on this site that was along the same lines – rm -rf – 2012-09-20T17:46:04.817

@r.m. I did read it, of course. My point is that the canonic question should be found when searching using keywords from the above question. I don't think that would work in this case. – Sjoerd C. de Vries – 2012-09-20T20:45:39.373

Answers

23

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]

Andrew

Posted 2012-09-19T20:22:30.267

Reputation: 9 123

8

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)

Sjoerd C. de Vries

Posted 2012-09-19T20:22:30.267

Reputation: 63 549

3Also, f[Sequence@@{a,b,c,d}] – 2012rcampion – 2015-03-19T17:42:55.320

4

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.

Mr.Wizard

Posted 2012-09-19T20:22:30.267

Reputation: 259 163

1Hold accepts any number of arguments, so putting them in a nested list is unnecessary. Then, that allows foo @@ args. – rcollyer – 2015-03-19T15:23:03.097

@rcollyer I know you know I know this. I guess I should have included that in the answer. Update soon... – Mr.Wizard – 2015-03-19T15:46:56.633

I blame laziness; on my part for pointing it out (and not editing it), etc. – rcollyer – 2015-03-19T15:48:04.090

@rcollyer lol :-) – Mr.Wizard – 2015-03-19T15:49:34.340