35
$\begingroup$

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.

$\endgroup$
14
  • 9
    $\begingroup$ f @@ {a, b, c, d}? For more, see the documentation for Apply. $\endgroup$ Sep 19, 2012 at 20:24
  • 2
    $\begingroup$ 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. $\endgroup$ Sep 19, 2012 at 20:28
  • 1
    $\begingroup$ @OleksandrR. I agree with closing this question. I have added a vote to close. $\endgroup$
    – Andrew
    Sep 19, 2012 at 20:38
  • 2
    $\begingroup$ @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. $\endgroup$ Sep 19, 2012 at 21:06
  • 1
    $\begingroup$ @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. $\endgroup$ Sep 19, 2012 at 21:18

3 Answers 3

29
$\begingroup$

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]

$\endgroup$
9
$\begingroup$

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)

$\endgroup$
2
  • 5
    $\begingroup$ Also, f[Sequence@@{a,b,c,d}] $\endgroup$ Mar 19, 2015 at 17:42
  • $\begingroup$ (warning: if a,b,c,d are objects that are themselves lists, the substitution will mess things up) $\endgroup$ Aug 10, 2023 at 21:02
4
$\begingroup$

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.

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

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.