How do I convert an argument list to a sequence of arguments?
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)
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.