35

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
Andrew
  • 10,569
  • 5
  • 51
  • 104
  • 9
    f @@ {a, b, c, d}? For more, see the documentation for Apply. – Oleksandr R. Sep 19 '12 at 20:24
  • 2
    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
  • 1
    @OleksandrR. I agree with closing this question. I have added a vote to close. – Andrew Sep 19 '12 at 20:38
  • Possible duplicate: http://stackoverflow.com/questions/5746717/apply-list-to-arguments-in-mathematica – Sjoerd C. de Vries Sep 19 '12 at 20:58
  • 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 Sep 19 '12 at 21:06
  • 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 Sep 19 '12 at 21:08
  • 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. Sep 19 '12 at 21:18
  • 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. Sep 19 '12 at 21:39
  • @OleksandrR. A list of recent easy questions in chat. Really close those too? – Sjoerd C. de Vries Sep 19 '12 at 22:12
  • @SjoerdC.deVries Here's a dupe on this site: http://mathematica.stackexchange.com/q/5432/5 – rm -rf Sep 20 '12 at 13:46
  • @r.m close, but not perfect. That question was about prefix. – Sjoerd C. de Vries Sep 20 '12 at 17:28
  • @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 Sep 20 '12 at 17:46
  • @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 Sep 20 '12 at 20:45
  • Related, possible duplicate: (4512) – Mr.Wizard Aug 03 '15 at 02:09

3 Answers3

29

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]

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Andrew
  • 10,569
  • 5
  • 51
  • 104
9

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
  • 65,815
  • 14
  • 188
  • 323
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
  • 271,378
  • 34
  • 587
  • 1,371