Suppose I have an array
p = {a,b,c,d}
and a function f that takes a variable number of arguments. I want to evaluate
f[a,b,c,d]
It won't do to type
f[p]
because this returns the array {f[a],f[b],f[c],f[d]}
which is not at all the right thing.
How do I get f to accept the elements of p (as opposed to p itself) as arguments?
Edited to add: Per a request in comments, here is a concrete example. Suppose p={2,3,4}. I would like an expression that returns Multinomial[2,3,4], which is to say 1260. It doesn't work to type Multinomial[p], because this gives {Multinomial[2],Multinomial[3],Multinomial[4]}={1,1,1}, which is not at all the same as 1260.
Answer
This is really easy if you understand the internal form of {a,b,c,d}
. Let's look at it:
p={a,b,c,d};
FullForm[p]
(* List[a,b,c,d] *)
as you see what you want is not really far away because basically, you only need to replace List
with f
. This is exactly what Apply
(or as operator @@
) does:
f @@ p
(* f[a, b, c, d] *)
Comments
Post a Comment